如何在Recycler视图中更新Recycler视图?

时间:2017-07-12 07:31:09

标签: android android-recyclerview recycler-adapter

我有一个回收站View作为父级,其中有一个回收站视图。当我从子回收器视图中选择一行时,我想更新所有父回收器视图的行。

在父级回收站视图中它有两个textview和一个回收站视图。父回收器视图的增量类和适配器类如下所示。

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rv = (RecyclerView)findViewById(R.id.reviews);


    LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
    rv.setLayoutManager(mLinearLayoutManager);
    Data();
     adapter = new Adapterclass(countryList,MainActivity.this);

    rv.setAdapter(adapter);
    rv.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(),rv,new ClickListener(){
        @Override
        public void onClick(View view, int position) {

            Log.e("position child",""+position);

            adapter = new Adapterclass(position);

        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));
}

适配器类:

public class Adapterclass extends RecyclerView.Adapter<Adapterclass.MyViewHolder> {

private List<ModelMain> flist;
private List<Modelclass>carList;
Context c;
boolean set = false;
int positn;

public class MyViewHolder extends RecyclerView.ViewHolder {


    TextView text,currency;

    RecyclerView recycler;
    public MyViewHolder(View view) {
        super(view);
        text = (TextView)view.findViewById(R.id.gggg);
        currency= (TextView)view.findViewById(R.id.hhh);
        recycler = (RecyclerView)view.findViewById(R.id.rview2);

    }

}
public  Adapterclass(List<ModelMain> slist, Context c){
    this.flist = slist;
    this.c = c;
}

@Override
public Adapterclass.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell,parent,false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(Adapterclass.MyViewHolder holder, final int position) {
    ModelMain fm = flist.get(position);

    holder.text.setText(fm.getName());
    holder.currency.setText(fm.getCurrency());

    carList = new ArrayList<>();
    carList = fm.getArrays();
    Log.d("list",""+carList.toString());
    LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(c);
    holder.recycler.setLayoutManager(mLinearLayoutManager);

    final Adapter_Inside adapter = new Adapter_Inside(carList,c);

    holder.recycler.setAdapter(adapter);

    holder.recycler.addOnItemTouchListener(new RecyclerTouchListener(c,holder.recycler,new ClickListener(){
        @Override
        public void onClick(View view, int pos) {
            ModelMain mm = flist.get(positn);

            carList = new ArrayList<>();
            carList = mm.getArrays();
            Modelclass mv = carList.get(pos);
            Log.e("valll", "" + carList.get(pos).getVal());

            Boolean bool = carList.get(pos).getVal();
            if(bool){

            }else{
                for(int i =0; i<carList.size();i++){
                    Modelclass cls = new Modelclass();
                 Boolean v = carList.get(i).getVal();
                    String text = carList.get(i).getText();
                    if(v){
                        cls.setVal(false);
                        cls.setText(text);

                        carList.set(i,cls);
                    }
                }
                mv.setVal(true);
                set  = true;
            }
            adapter.notifyDataSetChanged();


            }
       @Override
        public void onLongClick(View view, int position) {

        }
    }));


}
  public Adapterclass(int pos){
      this.positn = pos;


}
private void SetAdapr() {

    notifyDataSetChanged();
}

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private ClickListener clickListener;


    public RecyclerTouchListener(Context applicationContext, final RecyclerView rv, final ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(applicationContext, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = rv.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, rv.getChildPosition(child));

                }
            }
        });
    }


    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }

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

父回收者视图的模型类:

public class ModelMain {
String name;
String currency;
List<Modelclass> arrays ;

public ModelMain(String name, String currency, List<Modelclass> arrays) {
    this.name = name;
    this.currency = currency;
    this.arrays = arrays;
}

public String getName() {

    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCurrency() {
    return currency;
}

public void setCurrency(String currency) {
    this.currency = currency;
}

public List<Modelclass> getArrays() {
    return arrays;
}

public void setArrays(List<Modelclass> arrays) {
    this.arrays = arrays;
}
}

我的子回收器视图适配器类是:

public class Adapter_Inside extends      RecyclerView.Adapter<Adapter_Inside.MyViewHolder> {

  private List<Modelclass>carList;
    Context c;
    public class MyViewHolder extends RecyclerView.ViewHolder {


    TextView text;
    ImageView iv;


    public MyViewHolder(View view) {
        super(view);
        text = (TextView)view.findViewById(R.id.textView);
       iv = (ImageView)view.findViewById(R.id.iview);


    }

    }
public  Adapter_Inside(List<Modelclass> slist, Context c){
    this.carList = slist;
    this.c = c;
}

@Override
public Adapter_Inside.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.secondcell,parent,false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(Adapter_Inside.MyViewHolder holder, int position) {
    Modelclass fm = carList.get(position);

    holder.text.setText(fm.getText());
 if(fm.getVal()){
       holder.iv.setImageResource(R.drawable.ic_clear_black_18dp);
   }
    else{
       holder.iv.setImageResource(R.drawable.ic_done_black_16dp_2x);
   }
  }

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

孩子的模特班是:

public class Modelclass {

Boolean val;
String text;

public Boolean getVal() {
    return val;
}

public void setVal(Boolean val) {
    this.val = val;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}
}

单击子视图中的任何行时,我无法检索父回收器视图的位置。请帮助。

1 个答案:

答案 0 :(得分:0)

您可以通过在适配器构造函数中传递它来引用您的父级RecyclerView。

public  Adapterclass(RecyclerView parent, List<ModelMain> slist, Context c){
   this.flist = slist;
   this.c = c;
   this.recyclerView = parent;
}

然后您可以使用Recycler父引用及其方法来检索您需要的所有内容