如何在选中RecyclerView项目时突出显示并在Activity中使用其值

huangapple go评论73阅读模式
英文:

How to highlight a selected RecyclerView item and use its values in Activity

问题

public class MyListActivity extends AppCompatActivity {

    public ArrayList<MyWord> myWordList = new ArrayList<>();
    SelectData sd = new SelectData();
    int wordId;
    String word;
    String translation;

    public RecyclerView mRecycleView;
    public MyAdapter mAdapter;
    public RecyclerView.LayoutManager mLayoutManager;

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

        sd.selectMyWords(myWordList);

        buildRecycleView();

        Button btnInsert = findViewById(R.id.mywords_button_insert);
        Button btnEdit = findViewById(R.id.mywords_button_edit);
        Button btnDelete = findViewById(R.id.mywords_button_delete);
        Button btnMain = findViewById(R.id.mywords_button_backttomain);

        mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                wordId = myWordList.get(position).getWordId(); 
                word = myWordList.get(position).getWord();
                translation = myWordList.get(position).getTranslation();

                Toast.makeText(MyListActivity.this, "Selected -> " + word, Toast.LENGTH_SHORT).show();
            }
        });

        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(!(word == null)) {
                    Intent intentInsert = new Intent(MyListActivity.this, InsertActivity.class);
                    intentInsert.putExtra("W", word);
                    intentInsert.putExtra("TR", translation);
                    intentInsert.putExtra("ACTION", "ED");
                    startActivity(intentInsert);
                } else {
                    Toast.makeText(MyListActivity.this, "Select word", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void buildRecycleView(){
        mRecycleView = findViewById(R.id.mywords_RecycleView);
        mRecycleView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new MyAdapter(myWordList);

        mRecycleView.setLayoutManager(mLayoutManager);
        mRecycleView.setAdapter(mAdapter);

        if (myWordList.isEmpty()){
            Toast.makeText(this, "word list is empty", Toast.LENGTH_SHORT).show();
        }
    }
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private ArrayList<MyWord> mMyWordList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mListener = listener;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextViewEN;
        public TextView mTextViewLT;
        public ImageView imageViewLearned;

        public MyViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);
            mTextViewEN = itemView.findViewById(R.id.myList_textView_word);
            mTextViewLT = itemView.findViewById(R.id.myList_textView_translation);
            imageViewLearned = itemView.findViewById(R.id.iV_learned);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null){
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION){
                            listener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }

    public MyAdapter(ArrayList<MyWord> myWordList){
        mMyWordList = myWordList;
    }

    int selectedItem = -1;
   
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_list_view_layout, parent, false);
        MyViewHolder mvh = new MyViewHolder(v, mListener);
        return mvh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {

        MyWord currentMyWord = mMyWordList.get(position);
        holder.mTextViewEN.setText(currentMyWord.getWord());
        holder.mTextViewLT.setText(currentMyWord.getTranslation());
    }

    @Override
    public int getItemCount() {
        return mMyWordList.size();
    }
}
英文:

I have a list of words with translations in RecyclerView. I want the selected word to be highlighted, but at the same time I could use these values in Activity. I can highlight selected values or use the values of a selected item, but the combination of the both fails.
Please, maybe You see where I am wrong and can help solve my problem.

p.s. Please don’t judge me for the not enough clean code and my English, I’m learning. 如何在选中RecyclerView项目时突出显示并在Activity中使用其值

MyListActivity.java

public class MyListActivity extends AppCompatActivity {
public ArrayList&lt;MyWord&gt; myWordList = new ArrayList&lt;&gt;();
SelectData sd = new SelectData();
int wordId;
String word;
String translation;
public RecyclerView mRecycleView;
public MyAdapter mAdapter;
public RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list);
sd.selectMyWords(myWordList);
buildRecycleView();
Button btnInsert = findViewById(R.id.mywords_button_insert);
Button btnEdit = findViewById(R.id.mywords_button_edit);
Button btnDelete = findViewById(R.id.mywords_button_delete);
Button btnMain = findViewById(R.id.mywords_button_backttomain);
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
// **This is my selected item values**
wordId = myWordList.get(position).getWordId(); 
word = myWordList.get(position).getWord();
translation = myWordList.get(position).getTranslation();
Toast.makeText(MyListActivity.this, &quot;Selected -&gt; &quot; + word, Toast.LENGTH_SHORT).show();
}
});
btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!(word == null)) {
Intent intentInsert = new Intent(MyListActivity.this, InsertActivity.class);
intentInsert.putExtra(&quot;W&quot;, word);
intentInsert.putExtra(&quot;TR&quot;, translation);
intentInsert.putExtra(&quot;ACTION&quot;, &quot;ED&quot;);
startActivity(intentInsert);
} else {
Toast.makeText(MyListActivity.this, &quot;Select word&quot;, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
public void buildRecycleView(){
mRecycleView = findViewById(R.id.mywords_RecycleView);
mRecycleView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new MyAdapter(myWordList);
mRecycleView.setLayoutManager(mLayoutManager);
mRecycleView.setAdapter(mAdapter);
if (myWordList.isEmpty()){
Toast.makeText(this, &quot;word list is empty&quot;, Toast.LENGTH_SHORT).show();
}
}
}

MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt; {
private ArrayList&lt;MyWord&gt; mMyWordList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewEN;
public TextView mTextViewLT;
public ImageView imageViewLearned;
public MyViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mTextViewEN = itemView.findViewById(R.id.myList_textView_word);
mTextViewLT = itemView.findViewById(R.id.myList_textView_translation);
imageViewLearned = itemView.findViewById(R.id.iV_learned);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position);
}
}
}
});
}
}
public MyAdapter(ArrayList&lt;MyWord&gt; myWordList){
mMyWordList = myWordList;
}
int selectedItem = -1;
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_list_view_layout, parent, false);
MyViewHolder mvh = new MyViewHolder(v, mListener);
return mvh;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
MyWord currentMyWord = mMyWordList.get(position);
holder.mTextViewEN.setText(currentMyWord.getWord());
holder.mTextViewLT.setText(currentMyWord.getTranslation());
// TODO - START (select item highlight) - if I activate highlighting, the assignment of values no longer works in MyListActivity.
/*  holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedItem = position;
notifyDataSetChanged();
}
});
if (selectedItem == position) {
holder.itemView.setBackgroundColor(Color.parseColor(&quot;#C0C0C0&quot;));
} else {
holder.itemView.setBackgroundColor(Color.parseColor(&quot;#F8F8F8&quot;));
} */
//TODO -END
}
@Override
public int getItemCount() {
return mMyWordList.size();
}
}

答案1

得分: 0

看起来你的注释代码会覆盖点击监听器,只保留一个仅用于高亮的监听器。尝试将它们合并起来:

public MyViewHolder(@NonNull View itemView, final OnItemClickListener listener)   {
    super(itemView);
    mTextViewEN = itemView.findViewById(R.id.myList_textView_word);
    mTextViewLT = itemView.findViewById(R.id.myList_textView_translation);
    imageViewLearned = itemView.findViewById(R.id.iV_learned);

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (listener != null){
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION){
                    selectedItem = position;
                    notifyDataSetChanged();
                    listener.onItemClick(position);
                }
            }
        }
    });
}
英文:

Looks like your commented code would overwrite your click listener with one that only hightlights. Try combining them:

public MyViewHolder(@NonNull View itemView, final OnItemClickListener listener)   {
super(itemView);
mTextViewEN = itemView.findViewById(R.id.myList_textView_word);
mTextViewLT = itemView.findViewById(R.id.myList_textView_translation);
imageViewLearned = itemView.findViewById(R.id.iV_learned);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
selectedItem = position;
notifyDataSetChanged();
listener.onItemClick(position);
}
}
}
});
}

huangapple
  • 本文由 发表于 2020年7月27日 02:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63104055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定