In this tutorial explain how to used multiple listview in a single Activity / LinearLayout in android, we will explain below.
Utility.java
The Utility class show full listview according to list item length.
.........................................................................................................................................................
package com.Webnetware.adapter;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
res/layout/activity_main.xml
the xml design view class , you have to drag & drop two listView.
..............................................................................................................................................................
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/layout_blue"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#6bc4ce"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp"
android:layout_weight="1" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
the listview initialize and get Utility class in this and bind the listview via listviewobject
.................................................................................................................................................................
package com.androidbeginner.testcode;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView blue, red, green;
ArrayAdapter arrayAdapter;
private int checkedCounter=3;
ArrayList<String> ParameterList = new ArrayList<String>();
TextView text;
LayoutParams params = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1f);
@SuppressWarnings("rawtypes")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParameterList.add("item1");
ParameterList.add("item2");
ParameterList.add("item3");
ParameterList.add("item4");
ParameterList.add("item5");
ParameterList.add("item6");
blue = (ListView)findViewById(R.id.listView1);
red = (ListView)findViewById(R.id.listView2);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, ParameterList);
blue.setAdapter(arrayAdapter);
red.setAdapter(arrayAdapter);
Utility.setListViewHeightBasedOnChildren(blue);
Utility.setListViewHeightBasedOnChildren(red);
blue.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//blue.setItemChecked(0, true);
red.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//red.setItemChecked(0, true);
blue.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
checkedCounter-=1;
if(checkedCounter>=0){
Toast.makeText(getApplication(), ParameterList.get(position).toString()+"\n"+checkedCounter+"\n"+"less",Toast.LENGTH_LONG).show();
}
else {
}
}
});
red.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
checkedCounter-=1;
if(checkedCounter>=0){
Toast.makeText(getApplication(), ParameterList.get(position).toString()+"\n"+checkedCounter+"\n"+"less",Toast.LENGTH_LONG).show();
}else {
}
}
});
}
}
0 comments