implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.31' implementation "androidx.recyclerview:recyclerview:1.1.0" allproject maven { url "https://jitpack.io" } https://blog.csdn.net/qq_37237245/article/details/81021304 package com.example.myapplication;
public class User { private String name;
public String getName111() { return name; }
public void setName(String name) { this.name = name; }
public User(String name) { this.name = name; } }
package com.example.myapplication;
import android.util.Log; import android.view.View; import android.widget.BaseAdapter;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
public class BaseAdap extends BaseQuickAdapter<User, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener { public BaseAdap(int layoutResId, @Nullable List<User> data) { super(layoutResId, data); }
@Override protected void convert(BaseViewHolder helper, User item) { helper.setText(R.id.textview, item.getName111());
}
@Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { User user = (User) adapter.getItem(position); // 奇怪,为什么没有效果 Log.d("ddd", "clickwwwww "+ user.getName111());
} }
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList; import java.util.logging.Logger;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView; BaseAdap baseAdap ; ArrayList<User> arrayList = new ArrayList<>();
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycleview);
Button button = findViewById(R.id.button);
for(int i =0; i < 10;i++){ arrayList.add(new User("111:" + Integer.toString(i)));
}
baseAdap = new BaseAdap(R.layout.layout, arrayList); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // recyclerView.addItemDecoration(new SimplePaddingDecoration());
//添加Android自带的分割线 recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
// recyclerView.addItemDecoration();
baseAdap.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { User user = (User) adapter.getItem(position); Log.d("ddd", "click "+ user.getName111());
} });
recyclerView.setAdapter(baseAdap);
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("ddd","dfdfdfdfdf");
// baseAdap.remove(1); baseAdap.addData(new User("hahahs") ); recyclerView.setAdapter(baseAdap);
} });
} }