gpt4 book ai didi

android - 未调用 RecyclerView header 的 OnClickListener

转载 作者:行者123 更新时间:2023-11-29 18:33:14 25 4
gpt4 key购买 nike

我想在单击列标题时对我的 recyclerview 进行排序(例如,如果 Name 和 age 是 recyclerview 的两列,我想在单击标题 Age 时对列表进行排序)。我通过在包含 recyclerview 的 Activity 布局中包含标题布局来添加标题。

<RelativeLayout
android:id="@+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<include layout="@layout/header_item"/>

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="@null"
android:paddingBottom="@dimen/intrinsic_padding"
android:paddingTop="50dp"/>

</android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

在具有上述布局的 Activity 类中,我为 recyclerview 设置了适配器并为 Textview Age 添加了 onClickListener。

public class MainActivity extends AppCompatActivity {
TextView ageTextview;

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

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

ArrayList<User> usersList=initUsers();

// Create RecyclerView List
mRecyclerView=findViewById(R.id.my_recycler_view);

mRecyclerView.setHasFixedSize(true);

mLayoutManager=new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter=new RecyclerAdapter(usersList, getApplicationContext());
mRecyclerView.setAdapter(mAdapter);

ageTextview=findViewById(R.id.ageHeader);

ageTextview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Column -- Name", Toast.LENGTH_SHORT).show();

Collections.sort(usersList, new Comparator<User>() {
@Override
public int compare(User user1, User user2) {
if (user1.getAge() < user2.getAge())
return -1;
else if (user1.getAge()==user2.getAge())
return 0;
else
return 1;
}
});
mAdapter.notifyDataSetChanged();
}
});
}
}

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

private ArrayList<User> users;

private Context context;

RecyclerAdapter(ArrayList<User> users, Context context)
{
this.users=users;
this.context=context;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
return new VHItem(view);

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof VHItem) {
User user = users.get(position);

((VHItem)holder).userName.setText(user.getUserName());
((VHItem)holder).age.setText(String.valueOf(user.getAge()));
}
}

@Override
public int getItemCount() {
if (users !=null)
{
System.out.println("Number of users: "+ users.size());
return users.size();
}
else {
return 0;
}
}

public static class VHItem extends RecyclerView.ViewHolder {
public View view;
TextView userName;
TextView age;

VHItem(View view)
{
super(view);
this.view=view;
this.userName=view.findViewById(R.id.username);
this.age=view.findViewById(R.id.age);
}
}

当我点击年龄标题时,没有任何效果。既不会发生排序,也不会显示 Toast。看起来根本没有调用 onClick() 方法。日志没有显示任何错误。在布局文件 header_item 中,我尝试添加 android:clickable="true", android:focusable="false"。但没有任何效果。

如果我做错了什么,请帮助我理解。使用单独的布局文件添加 header 是否存在问题?

谢谢

最佳答案

尝试从 ViewHolder 获取 OnClickListner。像这样,

class ViewHolder extends RecyclerView.ViewHolder{

public TextView ageTextview;

public ViewHolder(View itemView) {

super(itemView);

ageTextview = temView.findViewById(R.id.ageTextview) ;

ageTextview.setOnClickListener(..);

}
}

关于android - 未调用 RecyclerView header 的 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55247539/

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