Development issue/problem:
I used this code to display two lists, one above the other.
The problem is that each of the two views in the list takes up half the screen.
I add a title to both lists, something like this.
LevelAdapter = new LevelAdapter(getActivity(),
R.layout.list_item, weather_data) ;
Header view = inflater.inflate(R.layout.header2, null);
Header view2 = inflater.inflate(R.layout.header, null);
lv1.addHeaderView(header);
lv2.addHeaderView(header2);
lv1.setAdapter(adapter);
lv2.setAdapter(adapter) ;
I want the title of the second list to appear after the end of the first. How do you do that? How can I make the second list start at the end of the first?
Thank you.
How can I solve this problem?
Solution 1:
Use this:
Scrap the linear format. Use a relative layout and place two list displays as shown.
Add Utility.java
social class utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter ==zero) {
// Condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasuresSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i
In your business:
lv1.setAdapter(adapter);
lv2.setAdapter(adapter) ;
Utility.setListViewHeightBasedOnChildren(lv1) ;
Utility.setListViewHeightBasedOnChildren(lv2) ;
Solution 2:
activity_basic.xml
MainActive.java
com.example.listviewin1xmldemo package ;
import android.os.bundle; import
android.support.v7.app.ActionBarActivity; import
android.view.view; import
android.view.MeasureSpec; import
android.view.ViewGroup; import
android.widget.ArrayAdapter; import
android.widget.ListAdapter; import
android.widget.ListView ;
The MainActivity public class extends the ActionBarActivity {.
private list mListView1, mListView2 ;
private string [] data1 = {Hiren, Pratik, Dhruv, Narendra, Piyush, Priyank} ;
private string [] data2 = {Kirit, Miral, Bhushan, Jiten, Ajay, Kamlesh} ;
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout.activity_main) ;
mListView1 = (ListView)findViewById(R.id.listView1);
mListView2 = (ListView)findViewById(R.id.listView2) ;
mListView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, data1));
mListView2.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, data2))
ListUtils.setDynamicHeight(mListView1);
ListUtils.setDynamicHeight(mListView2);
}
public static class ListUtils {
public static gap collectionDynamic height (ListView mListView) {
ListAdapter mListAdapter = mListView.getAdapter();
if (mListAdapter ==zero) {
// if adapter is null
return;
}
int height = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(mListView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i
Solution 3:
You must use ExpandableListView (http://developer.android.com/reference/android/widget/ExpandableListView.html). Instead of two list views, you have two sections, but they are aligned (including titles) as you described.
Solution 4:
Using multiple ListViews in the same container is not good practice. This leads to measurement and performance problems.
It is preferable to use a single list view with multiple view types. In your case, the view types can be: listview1_viewtype, listview2_viewtype, listview2_header_viewtype. Only the header can be used for the first header of ListView.
Solution no. 5:
When weights are specified, the height should be specified as 0dp or wrap_content. but you do not need to edit your xml file as shown below. I hope it works.
As you indicated in your comment, I am changing my position as requested.
1- : Create a list view with only 2 lines
1.1- : Add the list view as a child of the row in the first list view #1.
1.2- : Add another list view as child of the second line of the first list view #1.
I hope you can succeed this way.
Solution no. 6:
I’m also new to Android and I have something similar (not really, but kind of a workaround), but much easier by creating a layout file and putting the required number of views in it, and another XML format and wrapping the previously created layout in ScrollView.
Suppose you have a layout called two_listview_layout.xml which is defined as follows
RelativeLayout is optional here, but you can play around with your needs and layouts.
Now create another layout with this layout wrapped in ScrollView. Because ScrollView can only have one direct child, you as the child have this complete layout.
Name this layout (below) as final_layout.xml.
Now expand this final_layout.xml file into your snippet/activity and use the identifiers listed in the two_listview_layout.xml file to access one of the list views.
The output is something like this: (Sorry for the poor quality screenshot :p ) Trailers and reviews – both are lists of views here.
Solution no. 7:
Try using ScrollView and LinearLayout:
Active_main.xml
MainActive.java
private ListView list1, list2 ;
private String [] data1 ={H, P, D, N, P, P} ;
private String [] data2 ={K, M, B, K, A, K} ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sounds);
list1 = (ListView) findViewById(R.id.listView1);
list2 = (ListView) findViewById(R.id.listview2) ;
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,data1));
list2.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,data2))
}
Solution no. 8:
Adding layout_weight worked for me.
Solution no. 9:
Try a relative layout:
Good luck!
Related Tags:
how to combine two listview in android, multiple listview in scrollview android, two listview inside scrollview flutter, listview set multiple adapters, listview android, custom listview in android example, listview vs recyclerview, list to detail android studio, simple_list_item_1, android multi column listview arrayadapter, horizontal listview android, listview not showing android studio, grid view in android, android multiple views in one activity example, android multiple activities in one screen, xamarin forms how to display 2 listviews in one page, multiple xml in one activity in android, android listadapter example, two list view side by side android, android listview with different layouts for each row