gpt4 book ai didi

android - 如何使用 RecyclerView 的焦点项目做某事?

转载 作者:行者123 更新时间:2023-12-05 07:31:28 24 4
gpt4 key购买 nike

我有一个简单的 RecyclerView,每个项目只有一个 TextView。通过在 MainActivity 中使用 recyclerView.smoothScrollToPosition(myPosition);,我可以滚动到自定义项目。我需要的是在滚动之后更改滚动到的当前项目的 textColor。

例如,如果 myPosition 是 3 并且 RecyclerView 的第 3 项是“current”或“focused”,我想更改第 3 项的 textColor。

我该怎么做?

MainActivity.java:

public class MainActivity extends AppCompatActivity implements MyAdapter.ItemClickListener {

MyAdapter adapter;

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


ArrayList<String> itemTitle_List = new ArrayList<>();
itemTitle_List.add("facebook");
itemTitle_List.add("flickr");
itemTitle_List.add("google_plus");
itemTitle_List.add("instagram");
itemTitle_List.add("linkedin");
itemTitle_List.add("pinterest");
itemTitle_List.add("soundcloud");
itemTitle_List.add("swarm");
itemTitle_List.add("tumblr");
itemTitle_List.add("twitter");

// set up the RecyclerView
RecyclerView recyclerView = findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new MyAdapter(this, itemTitle_List);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);

recyclerView.smoothScrollToPosition(3);
}

@Override
public void onItemClick(View view, int position) {
//Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}
}

MyAdapter.java:

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

private List<String> itemTitle_List;
private LayoutInflater layoutInflater;
private ItemClickListener itemClickListener;

// data is passed into the constructor
MyAdapter(Context context, List<String> itemTitle_List) {
this.layoutInflater = LayoutInflater.from(context);
this.itemTitle_List = itemTitle_List;
}

// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.recyclerview_row, parent, false);
return new ViewHolder(view);
}


// total number of rows
@Override
public int getItemCount() {
return itemTitle_List.size();
}


// binds the title to the TextView, and image to the ImageView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String title = itemTitle_List.get(position);
holder.textView.setText(title);
}



// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textView;

ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (itemClickListener != null) itemClickListener.onItemClick(view, getAdapterPosition());
}
}

// convenience method for getting data at click position
String getItem(int id) {
return itemTitle_List.get(id);
}

// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}

// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>

</RelativeLayout>

recyclerview_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"/>

</LinearLayout>

最佳答案

我找到了一个简单的解决方案,但我不确定这是否是标准的。

MyAdapter.java 中添加一个名为 row_index=0 的公共(public)整数和一个公共(public)函数,如下所示:

public void selectRow(int index){
row_index=index;
notifyDataSetChanged();
}

然后在 MyAdapter.javaonBindViewHolder 函数中,添加以下代码:

if(position==row_index){
holder.textView.setTextColor(Color.RED);
}
else{
holder.textView.setTextColor(Color.BLACK);
}

然后,在 MainActivity.java 中的 recyclerView.smoothScrollToPosition(3) 之后,使用:

MyAdapter.selectRow(3);
MyAdapter.notifyDataSetChanged();

这样,当 RecyclerView 滚动到第 3 个项目时,该项目的 textColor 变为红色。

关于android - 如何使用 RecyclerView 的焦点项目做某事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com