新建.java文件,定义一个实体类 bt_list_adapter_type.java ,作为ListView适配器的适配类型;
public class bt_list_adapter_type{
private String name;
private int imageId;
public bt_list_adapter_type(String name,int imageId){
this.name=name;
this.imageId=imageId;
}
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
}
-----------------------------------------------------------------------
bt_list_item.xml
然后为ListView的子项指定一个自定义布局,在layout目录下新建bt_list_item.xml。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="6dp"
android:layout_marginLeft="16dp"
android:id="@+id/listview_image"
android:layout_width="20dp"
android:layout_height="20dp" />
<TextView
android:id="@+id/listview_text"
android:textSize="15dp"
android:padding="5dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.LinearLayoutCompat>
-----------------------------------------------------------------------
bt_list_adapter.java
创建一个自定义的适配器,新建.java文件,重写了父类的一组构造函数,用于将上下文、ListView子项布局的id和数据都传递进来,命名为bt_list_adapter.java;
package com.yk.sscom;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class bt_list_adapter extends ArrayAdapter<bt_list_adapter_type> {
private int resourceId;
public bt_list_adapter(Context context, int textViewResourceId, List<bt_list_adapter_type> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
bt_list_adapter_type glist_adapter_type=getItem(position); //获取当前项的实例
View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
ImageView listview_image=(ImageView)view.findViewById(R.id.listview_image);//设置显示的内容
TextView listview_text=(TextView)view.findViewById(R.id.listview_text);
listview_image.setImageResource(glist_adapter_type.getImageId());
listview_text.setText(glist_adapter_type.getName());
return view;
}
}
-----------------------------------------------------------------------
activity_main.xml
增加ListView, layout_width="match_parent"这样点击的时候就会有一整行被选中;
<ListView
android:id="@+id/list_bt"
android:layout_weight="1"
android:dividerHeight="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
-----------------------------------------------------------------------
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Switch switchComponent;
private ListView ListViewById;
List<bt_list_adapter_type> btList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Bt_log", "init ok");
init_listview_adapter();
}
private void init_listview_adapter() {
init_list();
bt_list_adapter adapter=new bt_list_adapter(MainActivity.this,R.layout.bt_list_item,btList);
ListView listview = (ListView) findViewById(R.id.list_bt);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//position 点击的Item位置,从0开始算
//String result = String.valueOf(parent.getCount());//列表有几项
bt_list_adapter_type itemAtPosition = (bt_list_adapter_type)parent.getItemAtPosition(position);//获取选择项的内容
Toast.makeText(MainActivity.this, "点击:" + result+" 内容:"+itemAtPosition.getName(), Toast.LENGTH_SHORT).show();
}
});
}
private void init_list() {
for(int i=0;i<2;i++){
bt_list_adapter_type apple=new bt_list_adapter_type("Apple",R.drawable.bt_png_1);
btList.add(apple);
bt_list_adapter_type orange=new bt_list_adapter_type("Orange",R.drawable.bt_png_1);
btList.add(orange);
bt_list_adapter_type banana=new bt_list_adapter_type("Banana",R.drawable.bt_png_1);
btList.add(banana);
bt_list_adapter_type waterlenmo=new bt_list_adapter_type("Waterlemon",R.drawable.bt_png_1);
btList.add(waterlenmo);
bt_list_adapter_type pear=new bt_list_adapter_type("Pear",R.drawable.bt_png_1);
btList.add(pear);
bt_list_adapter_type grape=new bt_list_adapter_type("Grape",R.drawable.bt_png_1);
btList.add(grape);
bt_list_adapter_type pineapple=new bt_list_adapter_type("Pineapple",R.drawable.bt_png_1);
btList.add(pineapple);
bt_list_adapter_type strawberry=new bt_list_adapter_type("Strawberry",R.drawable.bt_png_1);
btList.add(strawberry);
bt_list_adapter_type cherry=new bt_list_adapter_type("Cherry",R.drawable.bt_png_1);
btList.add(cherry);
bt_list_adapter_type mango=new bt_list_adapter_type("mango",R.drawable.bt_png_1);
btList.add(mango);
}
}
}
标签:layout,adapter,list,bt,studio,Android,ListView,type,android From: https://www.cnblogs.com/ysl666/p/17180858.html