web-dev-qa-db-ja.com

すべてのリサイクラービューアダプターのベースアダプターを作成する

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);

}

子クラスのホルダーを使用して、すべてのアイテム選択をクリックして実行するにはどうすればよいですか。

7
    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();
            }
        });
    }




}
1
rupali aggarwal

このサンプルアダプタクラスを参照して、要件に応じて編集できます。

public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder> {

List<abs.Ex> exList;
Context context;




absadapter(List exList) {
    this.exList= exList;

}
public static class exViewHolder extends RecyclerView.ViewHolder{
    CardView cardView;
    TextView exname;
    ImageView exlogo;


    exViewHolder(View itemView){
        super(itemView);
        cardView= itemView.findViewById(R.id.cardView);
        exname= itemView.findViewById(R.id.ex_name);
        exlogo=itemView.findViewById(R.id.exlogo);



    }


}
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
    absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
    return evh;


}


@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position){
    holder.exname.setText(exList.get(position).name);
    holder.exlogo.setImageResource(exList.get(position).logoId);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0 ){
                v.getContext().startActivity(new Intent(v.getContext(), abdetails.class));

            }
            if (position == 1) {
                v.getContext().startActivity(new Intent(v.getContext(), declinecrunch.class));


            }
            if (position == 2) {
                v.getContext().startActivity(new Intent(v.getContext(), dumsidebend.class));


            }
        }
    });


}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
    super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
    return  exList.size();
}

メインクラスについては、これを参照してください。

public class abs extends AppCompatActivity {


RecyclerView recyclerView;
Context context;
private List<Ex> exlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_abs);

    recyclerView= findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager=new LinearLayoutManager(context);
    recyclerView.setLayoutManager(layoutManager);

    initializeData();
    initializeAdapter();


}


private void initializeData(){

    exlist=new ArrayList<>();
    exlist.add(new abs.Ex("Crunches",R.drawable.crunchesgif));
    exlist.add(new abs.Ex("Decline Crunch",R.drawable.declinecrunch));
    exlist.add(new abs.Ex("Dumbell Side Bends",R.drawable.dumbbellsidebend));
    exlist.add(new abs.Ex("Hanging Leg Raises",R.drawable.hanginglegraises));
    exlist.add(new abs.Ex("Incline Leg Raises",R.drawable.inclinelegraises));
    exlist.add(new abs.Ex("Kneeling Cable Crunch",R.drawable.cablecrunch));
    exlist.add(new abs.Ex("Legs Raises",R.drawable.legraises));
    exlist.add(new abs.Ex("Flat Bench Lying Leg Raises",R.drawable.flatbenchlyinglegraise));
    exlist.add(new abs.Ex("Seated Jack Knife",R.drawable.seatedjackknife));
    exlist.add(new abs.Ex("Twisting Hip Raise",R.drawable.twistinghipraise));
    exlist.add(new abs.Ex("Weighted Crunch",R.drawable.weightedcrunch));
    exlist.add(new abs.Ex("Plank",R.drawable.plank));
    exlist.add(new abs.Ex("Side Plank",R.drawable.sideplank));
    exlist.add(new abs.Ex("Superman",R.drawable.superman));
    exlist.add(new abs.Ex("Twist Crunch",R.drawable.twistcrunch));


}
public void initializeAdapter(){
    absadapter rvadapter=new absadapter(exlist);
    recyclerView.setAdapter(rvadapter);
}
class Ex{
    String name;
    int logoId;

    Ex(String name,int logoId){
        this.name=name;
        this.logoId=logoId;
    }

}
1
user10169672

多くの方法がありますが、そのうちの1つは、 Java Generics を使用できます。

onCreateViewHolderでクリックを設定し、クリックを設定して、クリックされた行のリストアイテム、または必要なものを渡します。位置やその他のフィールドが必要な場合は、メソッドonClickViewsをカスタマイズできます。

@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
    ..
    itemView.setOnClickListener(v -> onClickViews(itemView, dataList.get(position)));
    ..
}

そして

public abstract void onClickViews(View view, T object);

のように使用

public class SampleAdapter extends BaseAdapters<Item> {

    @Override
    public int getRowLayout() {
        return R.layout.row;
    }

    @Override
    public void onClickViews(View view, Item object) {
        // row clicked   
    }
}

クラスを少し編集しました。

public abstract class BaseAdapters<T> extends RecyclerView.Adapter<BaseAdapters.MyViewHolder> {

    protected List<T> dataList = new ArrayList<>();

    public class MyViewHolder extends RecyclerView.ViewHolder {
        MyViewHolder(View view) {
            super(view);
            declareViews(view, this);
        }
    }

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

    public void notifyList(List<T> filteredNames) {
        dataList = filteredNames;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(getRowLayout(), parent, false);
        itemView.setOnClickListener(v -> onClickViews(itemView, dataList.get(position)));
        return new MyViewHolder(itemView);
    }

    public abstract @LayoutRes
    int getRowLayout();

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

    public abstract void onClickViews(View view, T object);

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

「BaseAdapters」のサブクラスでクリックイベントを処理するメソッドをオーバーライドする必要があります(他の投稿へのコメントに基づいて、bindView()がクリックイベントを処理すると思います。クリックイベントを処理することがわかっているメソッドをオーバーライドします)。

class new_adapter extends BaseAdapters{
    //...
    @Override
    public MyViewHolder bindView(MyViewHolder holder, int position){
        //you could alter how click events are handled here.
    }
    //...
}
1
kamyar haqqani

ベースリサイクラーアダプターは次のようになります

public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int BOTTOM_VIEW_TYPE = 3;

private List<Object> listModels;
private Context mContext;
private OnItemClickListener onClickListener = null;

public BaseRecyclerViewAdapter(Context context) {
    this.mContext = context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view;
    switch (viewType) {
    case BOTTOM_VIEW_TYPE:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_bottomview_type, parent, false);
            return new ChildViewHolder(view);
    }
    return null;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    final Object model = listModels.get(position);
    if (model != null && model.getId() > 0) {
        ((BaseViewHolder) holder).bind(model, position);
    } else if (model != null) {
        ((TopViewViewHolder) holder).bind(model, position);
    }
}

@Override
public int getItemViewType(int position) {
    if (listModels.get(position).getId() > 0) {
        return BASE_VIEW_TYPE;
    } else {
        return TOP_VIEW_TYPE;
    }
}

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


public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.onClickListener = onItemClickListener;
}
private class ChildViewHolder extends RecyclerView.ViewHolder {
    TextView tv_bottomName;

    BottomViewHolder(View v) {
        super(v);
        tv_bottomName = v.findViewById(R.id.tv_bottomName);
    }

    void bind(Object object, final int position) {
        tv_bottomName.setOnClickListener(view -> onClickListener.onItemClick(object, tv_bottomName, position));

    }
}

}

そして、OnItemClickListnerのカスタムインターフェイスを使用します

 public interface OnItemClickListener {
void onItemClick(Object object, View view, int position);}

そして、アクティビティまたはフラグメントにリスナーを実装します

0
gStephin