gpt4 book ai didi

java - 为所有 Recycler View Adapters 创建基础适配器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:52:53 25 4
gpt4 key购买 nike

public abstract class BaseAdapters extends RecyclerView.Adapter<BaseAdapters.MyViewHolder> implements View.OnClickListener {

protected Context parentContext;
public int layout_id;
protected List<?> dataList = new ArrayList<>();


public class MyViewHolder extends RecyclerView.ViewHolder {

MyViewHolder(View view) {
super(view);
declareViews(view,this);
}
}

@Override
public void onClick(View view) {
onClickViews(view);
}

@Override
public int getItemViewType(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
bindView(holder, i);
}

public void notifyList(List<?> filterdNames) {
this.dataList = filterdNames;
notifyDataSetChanged();
}

@Override
public int getItemCount() {
if (dataList.size() == 0)
return 5;
else
return dataList.size();
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout_id, parent, false);
return new MyViewHolder(itemView);
}

public abstract MyViewHolder bindView(MyViewHolder holder, int position);

public abstract void onClickViews(View view);

public abstract void declareViews(View view,MyViewHolder holder);

}

我如何使用扩展的子类中的 holder 单击每个项目选择。

最佳答案

    import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter extends RecyclerView.Adapter<BaseAdapter.MyViewHolder> {

public int layout_id;
protected List<?> dataList = new ArrayList<>();
Context BASE_CONTEXT;
public View itemview;

public BaseAdapter(Context context) {
this.BASE_CONTEXT = context;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout_id, viewGroup, false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder viewHolder, int position) {
onBindViewHold(position, dataList.get(position));
}

public abstract View getView(View view);

@Override
public int getItemCount() {
return dataList.size() == 0 ? 0 : dataList.size();
}


public abstract void onBindViewHold(int position, Object itemView);

class MyViewHolder extends RecyclerView.ViewHolder {


public MyViewHolder(@NonNull View itemView) {
super(itemView);
itemview = itemView;
getView(itemView);
}

}

public <T extends View> T bind(int id) {
return itemview.findViewById(id);
}


}

public class Adapter extends BaseAdapter {

TextView tv;

Adapter(Context context, ArrayList<String> arrayList){
super(context);
dataList=arrayList;
layout_id=R.layout.content_main2;

}


@Override
public View getView(View view) {
tv = bind(R.id.tv);
return view;
}

@Override
public void onBindViewHold( final int position, Object itemView) {

String text=(String) itemView;
Log.e("tv",tv.toString());
tv.setText(text);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BASE_CONTEXT, ""+position, Toast.LENGTH_SHORT).show();
}
});
}




}

关于java - 为所有 Recycler View Adapters 创建基础适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52664055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com