Android 如何在活动中处理RecyclerView外部的开关小部件(或按钮)
我用带有cardview的recyclerview加载数据,有两列,代码工作正常,但我需要用switch小部件隐藏第一列。我尝试了一些代码,但没有一个适合我。有人能帮我吗。我是android新手。谢谢 这是截图 下面是我的代码 MyFragment.javaAndroid 如何在活动中处理RecyclerView外部的开关小部件(或按钮),android,android-recyclerview,switch-statement,android-cardview,Android,Android Recyclerview,Switch Statement,Android Cardview,我用带有cardview的recyclerview加载数据,有两列,代码工作正常,但我需要用switch小部件隐藏第一列。我尝试了一些代码,但没有一个适合我。有人能帮我吗。我是android新手。谢谢 这是截图 下面是我的代码 MyFragment.java public class MyFragment extends Fragment { public MyFragment() { } @Override public View onCreateVie
public class MyFragment extends Fragment {
public MyFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup viewGroup,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview, viewGroup, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mobile = getResources().getStringArray(R.array.mobile);
desktop = getResources().getStringArray(R.array.desktop);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
MyAdapter adapter = new MyAdapter(getContext());
recyclerView.setAdapter(adapter);
// this code below didn't work. i thought it's because different
// layout between fragment and adapter but i don't know to solve it
final TextView tvMobile = (TextView) view.findViewById(R.id.tv_mobile);
Switch mSwitch = (Switch) view.findViewById(R.id.switch_item);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
tvMobile.setVisibility(View.GONE);
} else {
tvMobile.setVisibility(View.VISIBLE);
}
}
});
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static String[] mobile;
public static String[] desktop;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_cardview, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
holder.tvMobile.setText(mobile[position]);
holder.tvDesktop.setText(desktop[position]);
}
@Override
public int getItemCount() {
return mobile.length;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvMobile;
private TextView tvDesktop;
MyViewHolder(View itemView) {
super(itemView);
tvMobile = (TextView) itemView.findViewById(R.id.tv_mobile);
tvDesktop = (TextView) itemView.findViewById(R.id.tv_desktop);
}
}
}
MyAdapter.java
public class MyFragment extends Fragment {
public MyFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup viewGroup,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview, viewGroup, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mobile = getResources().getStringArray(R.array.mobile);
desktop = getResources().getStringArray(R.array.desktop);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
MyAdapter adapter = new MyAdapter(getContext());
recyclerView.setAdapter(adapter);
// this code below didn't work. i thought it's because different
// layout between fragment and adapter but i don't know to solve it
final TextView tvMobile = (TextView) view.findViewById(R.id.tv_mobile);
Switch mSwitch = (Switch) view.findViewById(R.id.switch_item);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
tvMobile.setVisibility(View.GONE);
} else {
tvMobile.setVisibility(View.VISIBLE);
}
}
});
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static String[] mobile;
public static String[] desktop;
public MyAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_cardview, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
holder.tvMobile.setText(mobile[position]);
holder.tvDesktop.setText(desktop[position]);
}
@Override
public int getItemCount() {
return mobile.length;
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvMobile;
private TextView tvDesktop;
MyViewHolder(View itemView) {
super(itemView);
tvMobile = (TextView) itemView.findViewById(R.id.tv_mobile);
tvDesktop = (TextView) itemView.findViewById(R.id.tv_desktop);
}
}
}
公共类MyAdapter扩展了RecyclerView.Adapter{
私人充气机;
公共静态字符串[]移动;
公共静态字符串[]桌面;
公共MyAdapter(上下文){
充气器=充气器。从(上下文);
}
@非空
@凌驾
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图=充气机。充气(R.layout.item_cardview,父项,false);
返回新的MyViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder,int位置){
holder.tvMobile.setText(移动[位置]);
holder.tvDesktop.setText(桌面[位置]);
}
@凌驾
public int getItemCount(){
返回移动长度;
}
类MyViewHolder扩展了RecyclerView.ViewHolder{
私有文本视图tvMobile;
私人文本视图电视桌面;
MyViewHolder(查看项目视图){
超级(项目视图);
tvMobile=(TextView)itemView.findviewbyd(R.id.tv\u mobile);
tvDesktop=(TextView)itemView.findViewById(R.id.tv_桌面);
}
}
}
fragment\u recyclerview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_light"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#dfe7f2"
android:gravity="end"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingRight="16dp"
android:paddingTop="5dp">
<Switch
android:id="@+id/switch_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Hide Mobile Row"
android:textColor="@color/text_color"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_divider" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp" />
</LinearLayout>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
card_view:cardBackgroundColor="@color/background_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="@drawable/bg_card_view" />
<TextView
android:id="@+id/tv_mobile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="romaji"
android:textColor="@color/black_1"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_desktop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="arti"
android:textColor="@color/black_1"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
item_cardwiew.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_light"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="#dfe7f2"
android:gravity="end"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingRight="16dp"
android:paddingTop="5dp">
<Switch
android:id="@+id/switch_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Hide Mobile Row"
android:textColor="@color/text_color"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_divider" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp" />
</LinearLayout>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
card_view:cardBackgroundColor="@color/background_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="@drawable/bg_card_view" />
<TextView
android:id="@+id/tv_mobile"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="romaji"
android:textColor="@color/black_1"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_desktop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="arti"
android:textColor="@color/black_1"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
在您的适配器中更改如下
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public static boolean isMobileHided = false; //add this flag
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
holder.tvMobile.setText(mobile[position]);
holder.tvDesktop.setText(desktop[position]);
if (isMobileHided) {
holder.tvMobile.setVisblity(View.GONE);
}
else{
holder.tvMobile.setVisblity(View.VISIBLE);
}
}
}
工作,先生,谢谢。因此,每次更改ViewHolder都应该使用这个方法notifyDataSetChanged()。是的,我们需要为每次更新使用notifyDataSetChanged()。如果答案是否定的,请在答案上打勾works@RanjithKumar谢谢你,老兄,你救了我这么多头痛!哈哈D