I want to create an Android App with Fragment
as a list item. Please someone help me if you know. Thanks in advance...

- 156,034
- 29
- 297
- 305

- 379
- 3
- 7
-
Why do you want to do this? – Mark Buikema Mar 10 '14 at 14:19
-
I need to show list of viewpager, and that viewpager contains list of images. – Prabhakaran Mar 10 '14 at 14:26
-
Just set new unique id to your container layout and you will able to add any fragment to you recyclerview item. This answer helped me. https://stackoverflow.com/a/42994810/1931613. For example, myContainerLayout.setId(SystemClock.currentThreadTimeMillis().toInt()) – Kiryl Ivanou Sep 06 '19 at 14:11
2 Answers
If you want to make like a custom list elements, For example, Setting you list items as another layout that contains more than one things like, ImagesViews, Text Views etc. Every item will have a different view. I am doing that something like this:
Step 1 : In my Adapter, I am setting the layouts as row elements.
Here's the code for that:
Here I have two different layouts.. One for the main row and other are the elements. (Other Layouts as you see in the getView()).
class yourAdapter extends BaseAdapter {
Context context;
String[] data;
private LayoutInflater inflater = null;
public yourAdapter(Context context, String[] data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.row, null);
if (position == 0) {
vi = inflater.inflate(R.layout.help_row1, null);
}
if (position == 1) {
vi = inflater.inflate(R.layout.help_row_2, null);
}
if (position == 2) {
vi = inflater.inflate(R.layout.help_row_3, null);
}
if (position == 3) {
vi = inflater.inflate(R.layout.help_row_4, null);
}
return vi;
}
}
Step 2 : This is how my onCreate() looks like:
Note : When I am setting the adapter, you can see that there are 4 strings as there are 4 layouts been used.
Now, instead of using just four layouts, you can have just one custom layout with any number of items in the listview with that custom layout. Depending on your array size or string array length. In this case, I am just using 4 layouts so I am not doing anything extra.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help);
TextView close = (TextView) findViewById(R.id.close_button);
close.setOnClickListener(this);
mActivity = this;
listview = (ListView) findViewById(R.id.helpListView);
mAdapter = new yourAdapter(this, new String[] { "Item1", "Item2",
"Item3", "Item4" });
listview.setAdapter(mAdapter);
}
Step 3 : The layouts been used for this example:
help.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/header_gradient"
android:gravity="center"
android:text="Help Page"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0099cc"
android:textSize="24sp"
android:textStyle="bold" />
<ListView
android:id="@+id/helpListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.63"
android:divider="#000000"
android:dividerHeight="6dp" >
</ListView>
<TextView
android:id="@+id/close_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/header_gradient"
android:gravity="center"
android:text="Close"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0099cc"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
row.xml : Using view here.
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</View>
And finally, one of the layouts:
help_row1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#666666">
<TextView
android:id="@+id/help_text_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Step 1:"
android:textColor="#0099cc"
android:layout_marginTop="10dp"
android:background="@drawable/header_gradient"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/help_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Destination by entering number or select the contact from your contact list."
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/help_text_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="For Example: Just Enter the number in the empty text field or simply select the destination by choosing contact from the contact list."
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:src="@drawable/asset1" />
</LinearLayout>
That's it. Now you can use this example to build your own custom listview. In your case, you can use fragments and use different data for your view.
Hope this example gives a bit of idea on how you can use fragment as row for your listview.
If someone can make this answer better, it will be very helpful for me too..thanks.:)

- 5,357
- 3
- 31
- 41