我真的需要你的帮助。我已经用很多关键字搜索Google了很多天,但我听不懂。所以,我决定问你。

所以,就在这里。实际上,我在RecyclerView中有一个按钮,但是该按钮会重复可用的尽可能多的数据,有:带有文本“ Baca 3x”,“ Baca 4x”等的按钮。我想要,如果我单击带有文本“ Baca 3x”的按钮3次,它将变为“ Baca 2x” >>“ Baca 1x” >>删除项目。另外,如果我单击带有文本“ Baca 4x”的按钮4次,它将变为“ Baca 3x” >>“ Baca 2x” >>“ Baca 1x” >>删除项目。

但是我的问题是,我不能对每个按钮进行不同的处理,因为每次删除该项目时,数据的位置都会自动更改。因此,我无法获得特定的按钮。例如:有两个按钮,

 1. Button "Baca 3x" on position 0
 2. Button "Baca 4x" on position 1


如果已删除位置0上的按钮“ Baca 3x”,则按钮“ Baca 4x”会自动将其位置更改为0。问题就在这里。

到目前为止,我只是根据其位置获得每个按钮,这对我来说是个问题。因此,我正在考虑如何在回收器视图中删除项目而不删除位置?你们能解决我的问题吗?我应该使用DiffUtil吗?如何使用?下面是我使用的完整代码:

ModelDoa.java

public class ModelDoa {

public static final int DOA_PAGI = 0;
public static final int DOA_SORE = 1;
public static final int DOA_MASJID = 2;
public static final int DOA_BANGUNT = 3;
public static final int DOA_MAU_TIDUR = 4;

private String mName;
private String bName;
private int mType;

public ModelDoa(String name, String butong, int type) {
    this.mName = name;
    this.bName = butong;
    this.mType = type;
}

public String getName() {
    return mName;
}

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


public int getType() {
    return mType;
}

public void setType(int type) { this.mType = type; }


public String ambilName() {
    return bName;
}

public void setNama(String butonk) {
    this.bName = butonk;
}

}


AdapterDoa.java

public class AdapterDoa extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public List<ModelDoa> mList;

public AdapterDoa(List<ModelDoa> list) {

    this.mList = list;
}

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

    switch (viewType) {

        case DOA_PAGI:
            View vieu = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
            PagiViewHolder rcv = new PagiViewHolder(vieu, this);
            return rcv;

        case DOA_SORE:
            View doa = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
            SoreViewHolder mdoa = new SoreViewHolder(doa);
            return mdoa;

        case DOA_MASJID:
            View dMasjid = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
            MasjidViewHolder mMasjid = new MasjidViewHolder(dMasjid);
            return mMasjid;

        case DOA_BANGUNT:
            View dBangunt = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
            BanguntViewHolder mBangunt = new BanguntViewHolder(dBangunt);
            return mBangunt;

        case DOA_MAU_TIDUR:
            View regut = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_doa, parent, false);
            MauTidurViewHolder turu = new MauTidurViewHolder(regut);
            return turu;
    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    ModelDoa object = mList.get(position);

    if (object != null) {

        switch (object.getType()) {

            case DOA_PAGI:
                ((PagiViewHolder) holder).mTitle.setText(object.getName());
                ((PagiViewHolder) holder).tombolbaca.setText(object.ambilName());
                break;

            case DOA_SORE:
                ((SoreViewHolder) holder).mTitle.setText(object.getName());
                ((SoreViewHolder) holder).tombolbaca.setText(object.ambilName());
                break;

            case DOA_MASJID:
                ((MasjidViewHolder) holder).mTitle.setText(object.getName());
                ((MasjidViewHolder) holder).tombolbaca.setText(object.ambilName());
                break;

            case DOA_BANGUNT:
                ((BanguntViewHolder) holder).mTitle.setText(object.getName());
                ((BanguntViewHolder) holder).tombolbaca.setText(object.ambilName());
                break;

            case DOA_MAU_TIDUR:
                ((MauTidurViewHolder) holder).mTitle.setText(object.getName());
                ((MauTidurViewHolder) holder).tombolbaca.setText(object.ambilName());
                break;
        }
    }
}

public void deleteItem(int position) {
    mList.remove(position); // hapus list
    notifyItemRemoved(position); // hapus tampilan
    // notifyItemRangeChanged( position, mList.size());
}

@Override
public int getItemCount() {
    if (mList == null)
        return 0;
    return mList.size();
}

@Override
public int getItemViewType(int position) {
    if (mList != null) {
        ModelDoa object = mList.get(position);
        if (object != null) {
            return object.getType();
        }
    }
    return 0;
}


}


PagiViewHolder.java

public class PagiViewHolder extends RecyclerView.ViewHolder {
public TextView mTitle;
public Button tombolbaca;
public Button teksbaca;
public Button tombolshare;
private RelativeLayout rl2;
private int klik10 = 10;
private AdapterDoa myAdapter;

public PagiViewHolder(View itemView, AdapterDoa myAdapter) {
    super(itemView);
    this.myAdapter = myAdapter;

    itemView.setOnClickListener(mainViewClickListener);
    mTitle = (TextView) itemView.findViewById(R.id.titleTextView);
    tombolbaca = (Button) itemView.findViewById(R.id.buttonbaca);
    tombolshare = (Button) itemView.findViewById(R.id.buttonshare);
    tombolbaca.setOnClickListener(bacaClickListener);
    tombolshare.setOnClickListener(shareClickListener);
    rl2 = (RelativeLayout) itemView.findViewById(R.id.relmasjid);
}

private View.OnClickListener bacaClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        teksbaca = (Button) v.findViewById(R.id.buttonbaca);

        // Baca 10x
        if( getAdapterPosition() ==0 ) {
            klik10--;
            teksbaca.setText("Baca " + klik10 + "x");

            if (klik10 <= 0)
            {
                // modify listItems however you want... add, delete, shuffle, etc
                myAdapter.deleteItem(getAdapterPosition());
            }
        }

    } // onclick
};

private View.OnClickListener shareClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // Do button click handling here
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, mTitle.getText().toString() + "\n \n download aplikasinya di: http://www.tauhid.or.id" );
            sendIntent.setType("text/plain");
            Intent.createChooser(sendIntent,"Share via");
            v.getContext().startActivity(sendIntent);
    }
};

private View.OnClickListener mainViewClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // Do button click handling here
    }
};


}


DoaPagi.java

public class DoaPagi extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_doa_pagi);

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //this line shows back button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    List<ModelDoa> rowListItem =  getData();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DoaPagi.this);
    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);
    AdapterDoa rcAdapter = new AdapterDoa(rowListItem);
    mRecyclerView.setAdapter(rcAdapter);

}

private List<ModelDoa> getData() {

    String[] data = getResources().getStringArray(R.array.doapagi);
    String[] baca = getResources().getStringArray(R.array.bacapagi);

    List<ModelDoa> list = new ArrayList<ModelDoa>();

    for (int i = 0; i < data.length; i++) {
            list.add(new ModelDoa(data[i], baca[i], ModelDoa.DOA_PAGI));
    }

    return list;
}

// Agar back button pada halaman induk settings berfungsi
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

}


更新(FIX CODE)通过:Krishna Sharma
https://github.com/seadclark/RecyclerViewWithButtonClicks

最佳答案

解决方法是这里。只需更新ModelDoa构造函数,如下所示。我已经验证了自己,并且现在可以按预期工作。还向您发送了在github上的拉取请求。

public ModelDoa(String name, String butong, int type) {
    this.mName = name;
    this.bName = butong;
    this.mType = type;
    String[] data = butong.split("\\s");
    if (data.length > 0) {
        String count = data[1].substring(0, data[1].length() - 1);
        read10 = Integer.parseInt(count);
    }
}

08-15 21:44