gpt4 book ai didi

android - 当点击 recyclerView 中的另一个位置时,setVisibility 如何消失所有其他位置?

转载 作者:行者123 更新时间:2023-11-30 00:11:32 24 4
gpt4 key购买 nike

我坚持了一个逻辑,无法达到预期的结果。我有一个回收器 View ,它有另一个水平的子回收器 View 。单击该项目后,我可以展开折叠 View 。我希望当我单击一个元素时,只有当前位置应该展开,所有其他位置都崩溃了。我尝试了很多逻辑,但没有人适合我。

我的回收站 View 适配器代码是:

public class NewAdpater extends RecyclerView.Adapter<NewAdpater.ViewHolder> implements Response.ErrorListener {
Context context;
boolean flag=true;
private List<FetchDocument> docDatalist;
int customerSerial;
private int expandedPosition = -1;

private int vinappserial;
ImageFetchResponse response;
ArrayList<Bitmap> bitmaps;
SendHoldertdata sendHoldertdata;
UtilityOfActivity utilityOfActivity;
List<ImageFetchResponseDocumentList> list = new ArrayList<>();


public NewAdpater(List<FetchDocument> docDatalist, Context context, String vinappserial, SendHoldertdata sendHoldertdata) {

this.docDatalist = docDatalist;
this.context = context;
this.sendHoldertdata = sendHoldertdata;
utilityOfActivity = new UtilityOfActivity((Activity) context);
this.vinappserial = Integer.valueOf(vinappserial);
UserDetail userDetail = new Gson().fromJson(PrefrenceFile.getInstance().getString(Constant.PREF_KEY_USER_DATA), UserDetail.class);
customerSerial = userDetail.getLoginResponse().getCustInfo().getCustSerialNo();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.doc_send_item_list, parent, false);

ViewHolder viewHolder = new ViewHolder(v);

return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.recyclerViewImages.setVisibility(View.GONE);
holder.doc_type.setText(docDatalist.get(position).getDocTypeDesc());

// expandedPosition=holder.getAdapterPosition();

holder.myDocumentListCollapse.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

boolean shouldExpand = holder.recyclerViewImages.getVisibility() == View.GONE;
if (shouldExpand) {
holder.recyclerViewImages.setVisibility(View.VISIBLE);

holder.iconexpandCollapse.setImageResource(R.mipmap.expand_4);
imagerequestserver(docDatalist.get(position), vinappserial, holder);
// DocumentImageFetchResponseListner setData= new DocumentImageFetchResponseListner(holder);

} else {

holder.recyclerViewImages.setVisibility(View.GONE);
holder.iconexpandCollapse.setImageResource(R.mipmap.expand);
}



}
});



}

@Override
public int getItemCount() {
return docDatalist.size();
}

public interface SendHoldertdata {
void setdata(ArrayList<Bitmap> bitmapArrayList, int count);
}

class ViewHolder extends RecyclerView.ViewHolder {
TextView doc_type;
LinearLayout myDocumentListCollapse;
RelativeLayout layoutAddDoc;
ImageView iconexpandCollapse;
RecyclerView recyclerViewImages;

public ViewHolder(View itemView) {
super(itemView);
doc_type = itemView.findViewById(R.id.doc_type);
myDocumentListCollapse = itemView.findViewById(R.id.myDocumentListCollapse);
layoutAddDoc = itemView.findViewById(R.id.layoutAddDoc);
iconexpandCollapse = itemView.findViewById(R.id.iconexpandCollapse);
recyclerViewImages = itemView.findViewById(R.id.recyclerViewImages);
recyclerViewImages.setVisibility(View.GONE);
}

}

public void imagerequestserver(FetchDocument fetchDocument, int vinappserial, ViewHolder holder) {

ImageFetchRequest imageFetchRequest = new ImageFetchRequest();
imageFetchRequest.setCustSerial(customerSerial);
imageFetchRequest.setDocGroup(fetchDocument.getDocTypeCode());
imageFetchRequest.setVehSerial(String.valueOf(vinappserial));
imageFetchRequest.setDocSerial("0");
String url = Constant.API_MAIN + Constant.API_DOCUMENT_IMAGEFETCH;
AbstractVolleyRequest jsonObjectrequest = new
AbstractVolleyRequest(url, this, new DocumentImageFetchResponseListner(fetchDocument, vinappserial, holder), imageFetchRequest, Request.Method.POST);
jsonObjectrequest.setTag("DocumentImageFetchRequest");
Log.v("DocumentFetchRequest", "DocumentImageFetchRequest" + jsonObjectrequest);
VolleyRequestQue.getmInstance(context.getApplicationContext()).addToRequestQueue(jsonObjectrequest);

}

@Override
public void onErrorResponse(VolleyError error) {
if (context != null) {
utilityOfActivity.progresDissmiss();
}
utilityOfActivity.toast(context.getString(R.string.connectivity_server_error));
}

public class DocumentImageFetchResponseListner extends MyListner<ImageFetchResponse> implements MailDocumentListAdapter.ISelectedImageMail {
FetchDocument fetchDocument;
String vinappserial;
public ViewHolder holder;


public DocumentImageFetchResponseListner(FetchDocument fetchDocument, int vinappserial, ViewHolder holder) {
super(ImageFetchResponse.class);
this.vinappserial = String.valueOf(vinappserial);
this.fetchDocument = fetchDocument;
this.holder = holder;
utilityOfActivity.progressDialogShow(context.getString(R.string.please_wait));
}


@Override
public void onResponse(MyResponse instaResponse) {
utilityOfActivity.progresDissmiss();
if (instaResponse != null) {
if (instaResponse instanceof ImageFetchResponse) {
response = (ImageFetchResponse) instaResponse;
if (response.getResult().getResultStatus().contains("SUCCESS")) {
list = response.getDocumentList();

// list.addAll(newList);
Log.d("", "");
if (list.size() > 0) {
setData();

}

} else {
utilityOfActivity.toast(context.getString(R.string.connectivity_server_error));
}
}
}
}

private void setData() {
MailDocumentListAdapter recyclerViewNewAdpater = new MailDocumentListAdapter(context, list, this);
holder.recyclerViewImages.setLayoutManager(new GridLayoutManager(context, 3));
recyclerViewNewAdpater.notifyDataSetChanged();
holder.recyclerViewImages.setAdapter(recyclerViewNewAdpater);
}

@Override
public void sendBitMap(ArrayList<Bitmap> itemSelectedArrayList,int count) {
bitmaps = itemSelectedArrayList;
sendHoldertdata.setdata(bitmaps,count);
}
}
}

这是我设置适配器的 DialogFragment:

public class DialogMailDocument extends DialogFragment implements NewAdpater.SendHoldertdata {
TextView txtCount;
int count=0;
ImageView documentCross;
RecyclerView recyclerViewDocument;
UtilityOfActivity utilityOfActivity;
AppCompatActivity appCompatActivity;
NewAdpater mainAdapter;
private int expandedPosition = -1;
Context mContext;
ArrayList<Bitmap> sendImageBitmap = new ArrayList<>();
List<ImageFetchResponseDocumentList> imageFetchResponseDocumentLists;
List<FetchDocument> fetchDocuments;
String vinappSerail;
MailDocumentListAdapter.ISelectedImageMail selectedImageMail;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.fullscreen_dialog);
}

private void init(View view) {
txtCount = view.findViewById(R.id.txtCount);
// selectAll = view.findViewById(R.id.selectAll);
recyclerViewDocument = view.findViewById(R.id.recyclerViewDocument);
TextView tvSendDoc = view.findViewById(R.id.tvSendDoc);
documentCross = view.findViewById(R.id.documentCross);
documentCross.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getDialog().dismiss();
}
});
tvSendDoc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sendImageBitmap != null) {
email(getActivity(), "xyz@gmail.com", "hello", sendImageBitmap);
} else {
Toast.makeText(getActivity(), "Nothing is selected", Toast.LENGTH_SHORT).show();
}
}
});

setDocumentRecyclerView();

}

public DialogMailDocument newInstance(MailDocumentListAdapter.ISelectedImageMail iSelectedImageMail) {
DialogMailDocument fragment = new DialogMailDocument();
this.selectedImageMail = iSelectedImageMail;
return fragment;
}


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
utilityOfActivity = new UtilityOfActivity(activity);
appCompatActivity = (AppCompatActivity) activity;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}

@Override
public void onStart() {
super.onStart();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_mail_document, container, false);
Bundle bundle = getArguments();
if (bundle != null) {
imageFetchResponseDocumentLists = bundle.getParcelableArrayList("documentList");
fetchDocuments = bundle.getParcelableArrayList("documentListHead");
vinappSerail = bundle.getString("vinappSerail");
}

init(view);
return view;
}

private void setDocumentRecyclerView() {
mainAdapter = new NewAdpater(fetchDocuments, getActivity(), vinappSerail, this,expandedPosition);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewDocument.setLayoutManager(mLayoutManager);
recyclerViewDocument.setAdapter(mainAdapter);
//
// GridLayoutManager manager =
// new GridLayoutManager(getActivity(), 3);
// recyclerViewDocument.setLayoutManager(manager);
// mainAdapter.setLayoutManager(manager);
// adapter.shouldShowHeadersForEmptySections(hideEmpty);
// adapter.shouldShowFooters(showFooters);
recyclerViewDocument.setAdapter(mainAdapter);
mainAdapter.notifyDataSetChanged();

}

public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

public void email(Context context, String emailTo, String emailText, ArrayList<Bitmap> filePaths) {
//need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("image/*");
emailIntent.putExtra(Intent.EXTRA_EMAIL,
new String[]{emailTo});
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (Bitmap file : filePaths) {
Uri u = getImageUri(getActivity(), file);
uris.add(u);
}

emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}


@Override
public void setdata(ArrayList<Bitmap> bitmapArrayList,int count) {
sendImageBitmap = bitmapArrayList;
this.count=count;
txtCount.setText(String.valueOf(count)+" selected");

}

@Override
public void setExpandedPosition(int expandedPosition) {
DialogMailDocument.this.expandedPosition = expandedPosition;
mainAdapter.notifyDataSetChanged();
}
}

最佳答案

在模型类中取一个 bool 值“isShow”,并将其默认设置为 false,用于您的 recyclerview 的所有项目。

然后....

if (list.get(position).isShow()) {
// show item....
} else {
// hide item....
}

在 onBindViewHolder 方法中重复一个 for 循环的最简单方法如下所示。

for (int i = 0; i < list.size(); i++) {
if (i == position) {
if (list.get(position).isShow()) {
list.get(position).setShow(false);
} else {
list.get(position).setShow(true);
}
} else {
list.get(i).setShow(false);
}
notifyDataSetChanged();
}

向我询问更多问题。

谢谢。

关于android - 当点击 recyclerView 中的另一个位置时,setVisibility 如何消失所有其他位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48056955/

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