使用界面

时间:2017-07-07 05:49:58

标签: android android-fragments android-recyclerview onclicklistener android-imagebutton

这个问题与我之前的问题有关 here

我有RecyclerView,显示了几个(8)CardView s。在每个CardView内,有几个元素,如TextViewImageButton等。我想要做的是更改分配给ImageButton的Vector(.xml)的颜色使用相同按钮的OnClickListener

我使用接口在Adapter类(RvAdapter扩展RecyclerView.Adapter)和片段之间进行通信。

public interface RvItemClickListener {
    void cardClick(View view, int position);
    void alarmClick(View view,int position);
}

在Adapter类中:

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
private List<Person> persons;
RvItemClickListener rvItemClickListener;
private Context mContext;

public RvAdapter(Context context,List<Person> persons) {
    this.mContext=context;
    this.persons=persons;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_content,parent,false);
    ViewHolder vh=new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ViewHolder mHolder=holder;
    mHolder.personName.setText(persons.get(position).name);
    mHolder.personAge.setText( persons.get(position).age);
}   

public void adapterSetClickListener(RvItemClickListener rvItemClickListener){
    this.rvItemClickListener=rvItemClickListener;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    CardView cvCard;
    TextView personName;
    TextView personAge;
    ImageButton alarmImageButton;


    public ViewHolder(View itemView) {
        super(itemView);
        cvCard=(CardView)itemView.findViewById(R.id.cv_card);
        personName=(TextView)itemView.findViewById(R.id.txt_person_name);
        personAge=(TextView)itemView.findViewById(R.id.txt_person_age);
        alarmImageButton=(ImageButton)itemView.findViewById(R.id.alarm_image_button);

        cvCard.setOnClickListener(this);
        alarmImageButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.cv_card:
                if(rvItemClickListener!=null)
                    rvItemClickListener.cardClick(view,getAdapterPosition());
                break;
            case R.id.alarm_image_button:
                if(rvItemClickListener!=null)
                    rvItemClickListener.alarmClick(view, getAdapterPosition());
                break;
            default:
                break;
        }
    }
}
}

在我的片段课程中:

public class Main_fragment extends Fragment implements RvItemClickListener{
  private RvAdapter myRecyclerAdaper;
  ImageButton alarmImageButton;
  private HashMap<Integer, Boolean> clickedItems;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     myRecyclerAdaper.adapterSetClickListener(this);
     clickedItems=new HashMap<>();
  }

  @Override
public void cardClick(View view, int position) {
    //openinig a new fragment.....this works fine
    getDataToBundle(view,position);
    nextFragmentInstance.setTargetFragment(this,0);
    nextFragmentInstance.setArguments(detailsToPass);
    this.getFragmentManager()
            .beginTransaction()
            .replace(R.id.content_main,newFragmentInstance,"tag")
            .addToBackStack(null).commit();
}

 @Override
 public void alarmClick(View view, int position) {
    //toast to make sure wheather the click event identify the position... works fine 
    Toast.makeText(getContext(),String.valueOf(position),Toast.LENGTH_SHORT).show();

   //changing the color of ImageButton vector using an instance of it
    alarmImageButton=(ImageButton)view.findViewById(R.id.alarm_image_button);
    if(!clickedItems.containsKey(position)) {
        alarmImageButton.setColorFilter(Color.argb(255, 255, 255, 255));
        clickedItems.put(position, true);
    }else{
        alarmImageButton.setColorFilter(Color.argb(0,0,0,0));
        clickedItems.remove(position);
    }
  }
}

我还没有给出Fragment类的完整代码。如果有人想我可以更新代码。

但问题与this one类似。唯一的区别是这次我使用界面代替onBindViewHolder()进行点击事件。 当我点击ImageButton时,其矢量颜色会与另一个ImageButton矢量一起更改。我尝试在我给出的链接中解决这个问题。但是我失败了。

0 个答案:

没有答案