当我点击recyclerView的某个项目时,其他项目也会改变其可见性。 我有一个recyclerView:
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/apps_installed_list"
android:name="com.campanon.bipbip.activities.ListAppsActivity"
android:layout_width="380dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layoutManager="LinearLayoutManager"
app:layout_constraintBottom_toTopOf="@+id/button_cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInstalledApps"
tools:listitem="@layout/app_installed_item_list">
行项目布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/app_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="5dp"
android:padding="3dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toStartOf="@+id/app_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/app_name"
android:layout_width="272dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/app_package"
app:layout_constraintStart_toEndOf="@+id/app_icon"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/app_package"
android:layout_width="272dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:gravity="center_vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/app_name"
app:layout_constraintTop_toBottomOf="@+id/app_name" />
<ImageView
android:id="@+id/selected_app"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/app_name"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_done_black_24dp" />
</android.support.constraint.ConstraintLayout>
当我点击某个项目时,最后一个ImageView(selected_app)会更改其可见性。我不知道为什么列表中的其他项目也只需点击一下即可更改可见性。
这是应用程序上的列表:
在适配器中,我使用holder.view实现了点击:
public class ListAppsInstalledAdapter extends RecyclerView.Adapter<ListAppsInstalledAdapter.ViewHolder> {
private List<ApplicationInfo> appsList = null;
private PackageManager packageManager;
private Context context;
CustomItemClickListener listener;
private static List<ApplicationInfo> appsSelected;
public static List<ApplicationInfo> getAppsSelected() {
return appsSelected;
}
public ListAppsInstalledAdapter(Context context, List<ApplicationInfo> appsList, CustomItemClickListener listener) {
this.appsList = appsList;
this.context = context;
this.packageManager = context.getPackageManager();
this.listener = listener;
appsSelected = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.app_installed_item_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final ApplicationInfo applicationInfo = appsList.get(position);
holder.appName.setText(applicationInfo.loadLabel(packageManager));
holder.packageName.setText(applicationInfo.packageName);
holder.iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.selectedApp.getVisibility() == View.INVISIBLE) {
holder.selectedApp.setVisibility(View.VISIBLE);
appsSelected.add(applicationInfo);
} else {
holder.selectedApp.setVisibility(View.INVISIBLE);
appsSelected.remove(applicationInfo);
}
listener.onItemClick(view, position);
}
});
}
@Override
public int getItemCount() {
return appsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final TextView appName;
public final TextView packageName;
public ImageView iconview;
public ImageView selectedApp;
public final View mView;
public ViewHolder(View view) {
super(view);
mView = view;
appName = (TextView) view.findViewById(R.id.app_name);
packageName = (TextView) view.findViewById(R.id.app_package);
iconview = (ImageView) view.findViewById(R.id.app_icon);
selectedApp = (ImageView)
view.findViewById(R.id.selected_app);
}
}
}
我不知道该怎么做:(
答案 0 :(得分:1)
看看是否有效:
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListAppsInstalledAdapter extends RecyclerView.Adapter<ListAppsInstalledAdapter.ViewHolder> {
private List<ApplicationInfo> appsList = null;
private PackageManager packageManager;
private Context context;
CustomItemClickListener listener;
private static List<ApplicationInfo> appsSelected;
private List<Integer> selectedPos = new ArrayList<>();
public static List<ApplicationInfo> getAppsSelected() {
return appsSelected;
}
public ListAppsInstalledAdapter(Context context, List<ApplicationInfo> appsList, CustomItemClickListener listener) {
this.appsList = appsList;
this.context = context;
this.packageManager = context.getPackageManager();
this.listener = listener;
appsSelected = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.app_installed_item_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final ApplicationInfo applicationInfo = appsList.get(position);
holder.appName.setText(applicationInfo.loadLabel(packageManager));
holder.packageName.setText(applicationInfo.packageName);
holder.iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
if(selectedPos.contains(position)){
holder.selectedApp.setVisibility(View.VISIBLE);
}else {
holder.selectedApp.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return appsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public final TextView appName;
public final TextView packageName;
public ImageView iconview;
public ImageView selectedApp;
public final View mView;
@Override
public void onClick(View v) {
Integer pos = getAdapterPosition();
if(selectedPos.contains(pos)){
selectedPos.remove(pos);
}else{
selectedPos.add(pos);
}
listener.onItemClick(v, getAdapterPosition());
}
public ViewHolder(View view) {
super(view);
mView = view;
appName = (TextView) view.findViewById(R.id.app_name);
packageName = (TextView) view.findViewById(R.id.app_package);
iconview = (ImageView) view.findViewById(R.id.app_icon);
selectedApp = (ImageView)
view.findViewById(R.id.selected_app);
view.setOnClickListener(this);
}
}
}
在您的接口实现中,调用适配器中的notifyDatasetChanged()
以使用所选位置重新加载recyclerview。