I am trying to make the switch from listview to recyclerview but can't seem to figure out how best to recycle views. What I would like is a set of cardviews in a cardview. The data set resembles a bunch of students in different classes. Every class has different sizes and they are to be represented in the same card.
What I have at the moment is the following xml where 'tv1' will contain the title of the class and the student names are dynamically added in 'card_ll'. The problem I am having is that I inflate the view for each student name manually and add / clear the linearlayout depending on how many students there are in the class. Note, the number of students is non-zero but cannot be determined beforehand.
Is there a way to reuse the views of each student card?
Thanks
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view1"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view2"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:id="#+id/card_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter onBindViewHolder implementation
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final ClassType ec = mDataset.get(position);
final String name = ec.classtitle;
holder.tv1.setText(ec.header1);
if( ec.studentlist!=null && ec.studentlist.size()>0) {
holder.cardll.removeAllViews();
for (Student s: ec.studentlist) {
TextView tv = new TextView(mContext);
tv.setText( s.getName());
tv.setTextColor(Color.BLACK);
holder.cardll.addView(tv);
}
}
}
Related
I have a work with the RecyclerView, and I use the CardView as well.I have made all these standards procedures (created the ViewHolder, implemented necessary methods) but the problem is that CardView's corners aren't rounded.I get only rectangular corners of the CardView.
Here is the XML code of Adapter's "main layout":
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="15dp"
card_view:cardElevation="10dp"
card_view:cardCornerRadius="30dp">
<RelativeLayout
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/adapterText"
android:text="#string/textCaption"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/adapterDate"
android:text="#string/date"
android:layout_marginTop="10dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/adapterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
And another layout with RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/contactList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
And initialization of the RecyclerView:
#Override
public void onViewCreated(View root, Bundle savedInstanceState) {
if (root != null) {
RecyclerView mRecyclerContactList=(RecyclerView)(root.findViewById(R.id.contactList));
mContactAdapter=new ContactAdapter(getContext(),mContextualMultiMode,mContactList);
mRecyclerContactList.setAdapter(mContactAdapter);
RecyclerView.ItemAnimator animator=new DefaultItemAnimator();
mRecycle
rContactList.setItemAnimator(animator);
RecyclerView.ItemDecoration mVerticalDecoration=new DividerItemDecoration
(getContext(), LinearLayoutManager.VERTICAL);
mRecyclerContactList.addItemDecoration(mVerticalDecoration);
mRecyclerContactList.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
Does anybody know how to solve this issue?
Cardview as root causes strange problems to me. Try to use Relativelayout as root.
Hi try using this code
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="250dp"
android:baselineAligned="false"
android:elevation="6dp"
cardview:cardCornerRadius="6dp"
cardview:cardElevation="6dp"
cardview:cardPreventCornerOverlap="false"
cardview:cardUseCompatPadding="true"
cardview:contentPadding="0dp">
</android.support.v7.widget.CardView>
Use this code when you are using cardview
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:elevation="100dp"
card_view:cardBackgroundColor="#color/cardview_initial_background"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
>
I'm getting an extra icon while adding ImageView in a ListView as shown below. What is this and how can I remove it?
In my content it also shows up and when selected it highlights the ImageView shown here
The xml code is here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="anaxin.newshellas.Feed"
tools:showIn="#layout/activity_feed">
<ListView
android:choiceMode="singleChoice"
android:listSelector="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/feedView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:clickable="false" />
<TextView
android:id="#+id/textTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/textBot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="#+id/textTop"
/>
<ImageView
android:layout_below="#+id/textTop"
android:layout_alignParentRight="true"
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
</RelativeLayout>
I use a SimpleAdapter with two rows (textTop, TextBot) to populate my listView like this:
final SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.content_feed,
new String[]{"First Line", "Second Line"},
new int[]{R.id.textTop, R.id.textBot}) {
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
};
And in method to add items this part
Map<String, String> datum = new HashMap<>(2);
datum.put("First Line", article.getTop());
datum.put("Second Line", article.getBot());
data.add(datum);
First of all Don't mix up ListView and other elements like TextView and ImageView which should be in the single item layout. Because of it you are seeing multiple images in single list.
And another thing to be noticed is your android:layout_below="#+id/textTop" attribute is conflicted in ImageView and TextView as well, so I suggest you to fix that as well.
Rest of the codes are fine enough !!
You have an extra ImageView and 2 extra TextViews in your layout file. These should be in the layout file of your list adapter and not in the layout containing listview itself. Try removing these extra views, that might solve your problem
remove all content in your list view like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="anaxin.newshellas.Feed"
tools:showIn="#layout/activity_feed">
<ListView
android:choiceMode="singleChoice"
android:listSelector="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/feedView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:clickable="false" />
you don't need them
I am adding views dynamically like this:
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamicabout, null);
TextView textOut = (TextView)addView.findViewById(R.id.aboutcontent);
textOut.setText(strings.get(index));
((ViewGroup) v).addView(addView);
However, once we get to the bottom of the screen and beyond, there's no scrollbar.
I tried using the ScrollView, but that doesn't do anything.
dynamicabout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
about.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/stdpadding"
android:paddingRight="#dimen/stdpadding"
android:paddingBottom="#dimen/stdpadding"
android:paddingTop="#dimen/stdpadding"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</LinearLayout>
What you need to do is you need to have Parent and Child view. Add child to the Parent. Parent is LinearLayout in ScrollView in dynamicabout.xml. Child is about XML. Inflate about XML on the runtime and Add it into Parent View (LinearLayout in ScrollView).
P.S. > Set an ID for the Parent Layout in order to be able to add views inside it, as far as I see it doesn't have ID.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/stdpadding"
android:paddingRight="#dimen/stdpadding"
android:paddingBottom="#dimen/stdpadding"
android:paddingTop="#dimen/stdpadding"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</LinearLayout>
////////////////////////////////////////////////
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
</RelativeLayout>
//////////////////////////////////////////////////////////////
LinearLayout linear=(LinearLayout)findviewbyId(R.id.linear);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamicabout, null);
TextView textOut = (TextView)addView.findViewById(R.id.aboutcontent);
textOut.setText(strings.get(index));
linearview.addView(addView);
please chk this code:
Why is there a Scroll Bar in List View even when the data in displayed in it doesn't require scrolling?
here is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
Comparing getLastVisiblePosition() with getCount() in a Runnable, you will get to know whether your list view will fit into the screen or not. Also check if the last visible row fits entirely on the screen or not.
ListView listView;
Runnable fitsOnScreen = new Runnable() {
#Override
public void run() {
int last = listView.getLastVisiblePosition();
if(last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) {
listView.setScrollContainer(false);
}
else {
listView.setScrollContainer(true);
}
}
};
Finally in ListView's Handler: onCreate()
listView.post(fitsOnScreen);
You can do this by
listView.setScrollContainer(false);
for more please check
How to get a non scrollable ListView?
If you are not want to hide the scrollbar for ever. Then it will help you.
Due to android:layout_height="match_parent" on listView and TextView scrollbar appears.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" // change 1
android:divider="#null"
android:dividerHeight="0dip" />
<TextView
android:id="#+id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" //change 2
android:gravity="center"
android:text="#string/no_result_text"
android:visibility="gone" />
</LinearLayout>
android:scrollbars="none"
Try putting the above line in your listview in xml.
I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion