我正在尝试将现有项目移植到材料设计中,但我无法理解RecyclerView的实现。我的旧代码使用ListView但我希望GMail应用程序像列表。
我已经阅读了大量的教程,但大多数都跳过ItemDecoration
部分或ClickListener
部分。不同的教程有完全不同的实现,代码各不相同,我无法理解在哪里放什么。
我在下面挣扎,我通过阅读各种教程编译:
适配器:我的适配器代码如下:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private final Context mContext;
private List<String> mData1, mData2;
public MyAdapter(Context mContext, String[] data1,String[] data2) {
this.mContext = mContext;
if (data1 != null)
mData1 = new ArrayList<String>(Arrays.asList(data1));
else mData1 = new ArrayList<String>();
if (data2 != null)
mData2 = new ArrayList<String>(Arrays.asList(data2));
else mData2 = new ArrayList<String>();
}
public void add(String s,int position) {
position = position == -1 ? getItemCount() : position;
mData1.add(position,s);
notifyItemInserted(position);
}
public void remove(int position){
if (position < getItemCount() ) {
mData1.remove(position);
notifyItemRemoved(position);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.list_item,viewGroup,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int position) {
myViewHolder.tv1.setText(mData1.get(position));
myViewHolder.tv2.setText(mData2.get(position));
myViewHolder.setClickListener(new MyViewHolder.ClickListener()
{
@Override
public void onClick(View v, int pos) {
Toast.makeText(mContext,"The selected position is: "+ pos,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mData1.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
protected TextView tv1;
protected TextView tv2;
ClickListener clickListener;
public MyViewHolder(View itemView) {
super(itemView);
tv1 = (TextView) itemView.findViewById(R.id.txt1);
tv2 = (TextView) itemView.findViewById(R.id.txt2);
}
@Override
public void onClick(View v) {
}
public void onClick(View v, int pos)
{
clickListener.onClick(v,getPosition());
}
public void setClickListener(ClickListener clickListener)
{
this.clickListener = clickListener;
}
public interface ClickListener
{
public void onClick(View v,int pos);
}
}
}
ItemDecoration
从此处复制:https://gist.github.com/alexfu/0f464fc3742f134ccd1e。但是我真的需要这么大的课程才能在列表项之间放置简单的分隔符吗?如果我想自己实现代码怎么办?
我仍然无法弄清楚OnClickListener
。首先,我想要一个简单的OnClickListener
,就像ListView.OnClickListener
一样。当前OnClickListener
是从教程编译但不起作用。
答案 0 :(得分:1)
RecyclerView适配器示例:
可以使用以下代码解决适配器问题:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
private final Context mContext;
private List<String> mData1, mData2;
public MyAdapter(Context mContext, String[] data1, String[] data2) {
this.mContext = mContext;
if (data1 != null)
mData1 = new ArrayList<String>(Arrays.asList(data1));
else
mData1 = new ArrayList<String>();
if (data2 != null)
mData2 = new ArrayList<String>(Arrays.asList(data2));
else
mData2 = new ArrayList<String>();
}
public void add(String s, int position) {
position = position == -1 ? getItemCount() : position;
mData1.add(position, s);
notifyItemInserted(position);
}
public void remove(int position) {
if (position < getItemCount()) {
mData1.remove(position);
notifyItemRemoved(position);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View itemView = inflater.inflate(R.layout.list_item, viewGroup, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, final int position) {
myViewHolder.tv1.setText(mData1.get(position));
myViewHolder.tv2.setText(mData2.get(position));
myViewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clickListener.onClick(v, position);
}
});
}
@Override
public int getItemCount() {
return mData1.size();
}
ClickListener clickListener;
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public interface ClickListener {
public void onClick(View v, int pos);
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
protected TextView tv1;
protected TextView tv2;
public MyViewHolder(View itemView) {
super(itemView);
tv1 = (TextView) itemView.findViewById(R.id.txt1);
tv2 = (TextView) itemView.findViewById(R.id.txt2);
}
}
}
用法如下:
MyAdapter adapter=....;
adapter.setClickListener(new ClickListener() {
@Override
public void onClick(View v, int pos) {
// do whatever you want
}
});
没有简单的方法可以做到这一点。但我建议你解决这个问题。在list_item.xml
内的底部添加以下视图作为分隔符。
<View
android:layout_width="wrap_content"
android:layout_height="0.5dip"
android:background="@color/light_gray"/>
项目点击监听器也在点号中解决。 1以上。
我希望这会对你有所帮助。
答案 1 :(得分:0)
以下是使用简单库简单实施RecyclerView的示例
在contract ProxyController{
ContractA a;
ContractB b;
constructor() public {
a=new ContractA();
b=new ContractB();
}
function AgetValue() public view returns (string){
return a.getValue();
}
function BgetValue() public view returns (string){
return b.getValue();
}
}
build.gradle
使用
在Activity中创建RecyclerView布局implementation 'com.hereshem.lib:awesomelib:2.0.1'
通过传递它支持的类
来创建一个ViewHolder<com.hereshem.lib.recycler.MyRecyclerView
android:id="@+id/recycler"
app:layoutManager="LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
创建项目列表变量和包含很少行的适配器
public static class EVHolder extends MyViewHolder<Events> {
TextView date, title, summary;
public EVHolder(View v) {
super(v);
date = v.findViewById(R.id.date);
title = v.findViewById(R.id.title);
summary = v.findViewById(R.id.summary);
}
@Override
public void bindView(Events c) {
date.setText(c.date);
title.setText(c.title);
summary.setText(c.summary);
}
}
ClickListener可以添加以下行
List<Events> items = new ArrayList<>();
MyRecyclerView recycler = findViewById(R.id.recycler);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, items, EVHolder.class, R.layout.row_event);
recycler.setAdapter(adapter);
可以找到更多示例here
希望这会有所帮助:)