老项目不能用recyclerview,用的recyclerview就项目加载不出数据,当然可能是我版本用的低。。但是用高版本就报错,无法运行项目,可能是我自己没有配好。 闲话少说,公司项目需要做单选和多选的,就做一个记录
下面直接贴代码:
首先是adapter:
public class Myadapter extends BaseAdapter{
private Context context;
private ArrayList<User> datas=new ArrayList<>();
private int TagPostion=-1;//默认未选中
public ComPanyAdapter(Context context, ArrayList<User> list,int tagPostion) {
this.context=context;
this.datas=list;
this.TagPostion=tagPostion;
}
//这里是用来回传,调用这里让标识刷新,否则标识无法传进来,无法刷新,则不显示对应的已勾选单选标识
public void setTagPostion(int TagPostion){
this.TagPostion=TagPostion;
notifyDataSetChanged();
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView==null){
viewHolder=new ViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.item_company,null);
viewHolder.apace_item= (TextView) convertView.findViewById(R.id.item_tv);
viewHolder.apace_image= (ImageView) convertView.findViewById(R.id.apace_image);
convertView.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) convertView.getTag();
}
viewHolder.apace_item.setText(datas.get(position).getName());
if (TagPostion==position){
viewHolder.apace_image.setVisibility(View.VISIBLE);
viewHolder.apace_image.setImageResource(R.drawable.gou);
}else {
viewHolder.apace_image.setVisibility(View.GONE);
}
return convertView;
}
static class ViewHolder {
TextView apace_item;
ImageView apace_image;
}
}
这里是对应的Bean:
public class User{
private int id;
private String name;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public string getName(){
return name;
}
public void setName(string name){
this.name=name;
}
}
对应的activity:
//这里的BaseActivity改成自己项目的activity基类....别啥都不看就复制。。没有写基类就用系统自带的ActivityCompat
public class MainActivity extends BaseActivity{
private ListView list;
private ArrayList<User> datas;
private String userName; //要回传的参数
private int TagPosition=-1;//默认未选中为-1;
private string selectedString="";//已经选中的数据
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_building);
Intent intent = getIntent();
//上个界面获取到的数据(可无视)
selectedString= intent.getStringExtra("company");
initView();
//数据
getData();
}
private void initView(){
list = (ListView) findViewById(R.id.list_view);
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
adapter=new MyAdapter(MainActivity.this, datas,TagPosition);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TagPosition = position;
adapter.setTagPostion(TagPosition);
}
});
}
private ArrayList<User> getData(){
users=new ArrayList<>();
for (int i=0;i<20;i++){
User user=new User();
user.setId(i);
user.setName("张三"+i);
datas.add(user);
}
return datas;
}
}
xml文件布局(很简单)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_below="@id/include_head"
android:layout_width="match_parent"
android:divider="#eeeeee"
android:dividerHeight="0.5dp"
android:fadingEdge="none"
android:layout_height="match_parent"/>
</RelativeLayout>
对应item布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="7dp"
android:background="@drawable/list_view_item_bg"
android:paddingBottom="7dp"
android:layout_height="80dp">
<TextView
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:textSize="24dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/apace_image"
android:layout_width="wrap_content"
android:background="@drawable/gou"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
image随便去阿里图库找一个勾就好了。。。图标就不上传了
以上 结束