ListView, ArrayAdapter, and arrayadapter
Usage Summary of Listview adapter ArrayAdapter
The following code uses the chat dialog Function Code
1. First define an object class as the wildcard of ArrayAdapter
public class ChatlistviewData { public static final int LEFT_CHAT=1; public static final int RIGHT_CHAT=2; private int type; private String content; public int getType() { return type; }
public String getContent() { return content; }
// Method 1
public void setContent(String content) { this.content = content; } public void setType(int type) { this.type = type; }
// Method 2
public ChatlistviewData(String content,int type){
this.content = content;
this.type = type;
}}
2. Add the ListView control to the layout file, and add other controls to make the layout look as desired.
Xml version = "1.0" encoding = "UTF-8">
<LinearLayout
Xmlns: android = "https://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: padding = "6sp"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ListView
Android: id = "@ + id/m_listview"
Android: layout_weight = "1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>
<LinearLayout
Android: gravity = "center_vertical"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
<EditText
Android: layout_weight = "1"
Android: id = "@ + id/et_input"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_sendtext"
Android: textColor = "@ color/white_color"
Android: background = "@ drawable/button_bg"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "send"/>
LinearLayout>
LinearLayout>
3. Create a layout as a subitem of the ListView.
xml version="1.0" encoding="utf-8"><LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/left_layout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/white_color" android:background="@drawable/chat_bg_cloud" /> LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right"> <TextView android:id="@+id/tv_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/white_color" android:background="@drawable/chat_bg_user" /> LinearLayout> LinearLayout>
4. Create your own adapter and inherit from ArrayAdapter. The generic type is the entity class defined in step 1.
public class Adapter_chat extends ArrayAdapter { private int resouurId; public Adapter_chat(Context context, int textViewResourceId,List objects) { super(context,textViewResourceId, objects); resouurId=textViewResourceId; } @Override public View getView( int position, View convertView, ViewGroup parent) { ChatlistviewData data=getItem(position); View view; ViewHoler viewHoler; if(convertView== null){ view= LayoutInflater. from(getContext()).inflate( resouurId, null); viewHoler= new ViewHoler(); viewHoler. left_layout=(LinearLayout)view.findViewById(R.id. left_layout); viewHoler. right_layout=(LinearLayout)view.findViewById(R.id. right_layout); viewHoler. tv_left=(TextView)view.findViewById(R.id. tv_left); viewHoler. tv_right=(TextView)view.findViewById(R.id. tv_right); view.setTag(viewHoler); } else { view=convertView; viewHoler=(ViewHoler)view.getTag(); } if(data.getType()==ChatlistviewData. LEFT_CHAT){ viewHoler. left_layout.setVisibility(View. VISIBLE); viewHoler. right_layout.setVisibility(View. GONE); viewHoler. tv_left.setText(data.getContent()); } else if(data.getType()==ChatlistviewData. RIGHT_CHAT){ viewHoler. left_layout.setVisibility(View. GONE); viewHoler. right_layout.setVisibility(View. VISIBLE); viewHoler. tv_right.setText(data.getContent()); } return view; } class ViewHoler{ LinearLayout left_layout; LinearLayout right_layout; TextView tv_right; TextView tv_left; }}
This is the first complex part of ListView.
First, write a constructor. The textViewResourceId In the constructor is the id of the ListView subitem layout. List Object is all the sub-items you want to add to the ListView. When adding data, we can also customize an ArrayList to put all our sub-items in it.
The second step is to add a getView () method, which enables each ListView subitem to be displayed in the ListView when sliding the screen. First, get the instance of the current item through the getItem () method, then, LayoutInflater is used to load the layout we passed in for this subitem, And the findViewById () method of View is called to put the control contained in the subitem of ListView, finally, set the style of the control in each ListView subitem. The above is the specific idea. There are several details to explain. The convertView parameter is used to cache the previously loaded layout so that it can be reused later, viewHolder does not allow the program to call the findViewById () method of View every time the getView () method is used, and the setTag (), getTag (), setTag () the Object is stored as a parameter for the view. That is to say, we need to extract the control in an itemView into an Object. Here, we create a ViewHolder and then get the corresponding object through getTag (key.
5. the last step is to add the control ListView on the ListView layout interface to create a List and add the data to it. Bind The adpter with the ListView by using the Adapter defined before instantiation. The last step is not to add the code. It shows two useful ones,MyAdapter. NotifyDataSetChanged (); refresh ListViewMlistview. SetSelection (List. Size (); enables ListView to automatically slide to the last row
You do not need to check the click event here.
After all, the scrolling of ListView only satisfies our visual effect. However, if the subitem in ListView cannot be clicked, this control has no practical purpose. Therefore, in this section, we will learn how ListView can respond to user click events. Modify the code in MainActivity as follows: public class MainActivity extends Activity {
private List fruitList = new ArrayList ();
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits(); FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView parent, View view, int position, long id) { Fruit fruit = fruitList.get(position); Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show(); }
});
} ……
} We can see that we use the setOnItemClickListener () method to register a listener for ListView. When a user clicks any subitem in ListView, The onItemClick () method is called back, in this method, you can use the position parameter to determine which sub-item the user clicks, obtain the corresponding fruit, and display the fruit name through Toast.