我在列表视图的每个元素内使用列表视图。
<?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="wrap_content"
android:padding="@dimen/tiny_paddding"
>
<LinearLayout
android:id="@+id/fileNameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="140dp"
android:orientation="vertical"
android:layout_alignParentStart="true">
<TextView
style="@style/NormalBoldText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="File Name" />
<TextView
android:id="@+id/fileName"
style="@style/NormalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="140dp"
android:ellipsize="end"
android:maxLines="1"
android:text="HelloFriends" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/fileNameLayout"
android:layout_marginLeft="@dimen/small_margin"
android:maxWidth="140dp"
android:orientation="vertical">
<TextView
style="@style/NormalBoldText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="File Id" />
<TextView
android:id="@+id/fileId"
style="@style/NormalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxWidth="140dp"
android:maxLines="1"
android:text="HelloFriends123" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:weightSum="2"
android:layout_alignParentEnd="true">
<TextView
android:id="@+id/fileUploadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/ic_file_upload"
android:backgroundTint="@color/blue" />
<TextView
android:id="@+id/fileDeleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/small_margin"
android:layout_weight="1"
android:background="@drawable/ic_delete"
android:backgroundTint="@color/red" />
</LinearLayout>
</RelativeLayout>
因此,对于父级列表中的每个列表项,我都必须为父级列表中的列表创建一个适配器。
public class Document {
public String documentName;
public DOCUMENT documentType;
public boolean multiFile;
public DocumentUploadListAdapter documentUploadListAdapter;
}
现在的问题是事件,尽管我为每个文档(父项)提供了不同的列表适配器,但我最终还是以相同的列表项结束了。
子列表:
@Data
public class DocumentUploadListAdapter extends BaseAdapter implements Comparable<DocumentUploadListAdapter>{
public static final String TAG = DocumentUploadListAdapter.class.getSimpleName();
private List<DocumentUpload> documentUploadList;
public DOCUMENT documentType;
private Context context;
private DocumentUploadListAdapter documentUploadListAdapter;
private DocumentUploadListClickInterface documentUploadListClickInterface;
public DocumentUploadListAdapter(){
super();
documentUploadListAdapter = this;
}
@Override
public int getCount() {
if(documentUploadList==null){
return 0;
}
return documentUploadList.size();
}
@Override
public DocumentUpload getItem(int position) {
return documentUploadList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
DocumentUploadListViewModel documentUploadListViewModel = null;
final DocumentUpload documentUpload = documentUploadList.get(position);
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
documentUploadListViewModel = new DocumentUploadListViewModel();
rowView = inflater.inflate(R.layout.list_item_upload_document,null);
documentUploadListViewModel.filedId = rowView.findViewById(R.id.fileId);
documentUploadListViewModel.fileName = rowView.findViewById(R.id.fileName);
documentUploadListViewModel.fileUploadButton = rowView.findViewById(R.id.fileUploadButton);
documentUploadListViewModel.fileDeleteButton = rowView.findViewById(R.id.fileDeleteButton);
documentUploadListViewModel.fileUploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!documentUpload.isUploaded()){
//upload the document here
documentUploadListClickInterface.onDocumentUploadListUpload(documentUploadListAdapter, documentUploadList.get(position));
}
}
});
documentUploadListViewModel.fileDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//check the delete button
//needs to checked and the views need to be refreshed
documentUploadList.remove(documentUploadList.get(position));
notifyDataSetChanged();
}
});
rowView.setTag(documentUploadListViewModel);
}
documentUploadListViewModel = (DocumentUploadListViewModel) rowView.getTag();
documentUploadListViewModel.filedId.setText(documentUpload.getFileId()+"");
documentUploadListViewModel.fileName.setText(documentUpload.getFileName());
if(documentUpload.isUploaded()){
Log.d(TAG, "Image is uplaoded, changing the image");
documentUploadListViewModel.fileUploadButton.setBackground(getContext().getResources().getDrawable(R.drawable.ic_check_circle, null));
documentUploadListViewModel.fileUploadButton.setBackgroundTintList(ContextCompat.getColorStateList(getContext(), R.color.green));
documentUploadListViewModel.fileUploadButton.setEnabled(false);
}
else{
Log.d(TAG, "Reversing, changing the image");
documentUploadListViewModel.fileUploadButton.setBackground(getContext().getResources().getDrawable(R.drawable.ic_file_upload, null));
documentUploadListViewModel.fileUploadButton.setBackgroundTintList(ContextCompat.getColorStateList(getContext(), R.color.blue));
documentUploadListViewModel.fileUploadButton.setEnabled(true);
}
return rowView;
}
public void addDocumentUpload(DocumentUpload documentUpload){
if(documentUploadList==null){
documentUploadList = new ArrayList<>();
}
documentUploadList.add(documentUpload);
this.notifyDataSetChanged();
}
public List<DocumentUpload> getDocumentUploadList(){
return documentUploadList;
}
@Override
public int compareTo(DocumentUploadListAdapter o) {
if(this.documentType.compareTo(o.documentType) == 0) return 0;
return 1;
}
public class DocumentUploadListViewModel{
public TextView filedId;
public TextView fileName;
public TextView fileUploadButton;
public TextView fileDeleteButton;
}
public interface DocumentUploadListClickInterface{
void onDocumentUploadListUpload(DocumentUploadListAdapter documentUploadListAdapter, DocumentUpload documentUpload);
}
}
父母名单:
@Data
public class DocumentListAdapter extends BaseAdapter implements DocumentUploadListAdapter.DocumentUploadListClickInterface {
public static final String TAG = DocumentListAdapter.class.getSimpleName();
private List<Document> documentList;
private Context context;
private DocumentListClickInterface documentListClickInterface;
@Override
public int getCount() {
if(documentList==null){
return 0;
}
return documentList.size();
}
@Override
public Document getItem(int position) {
return documentList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
final Document document = documentList.get(position);
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item_documents,null);
DocumentViewHolder documentViewHolder = new DocumentViewHolder();
documentViewHolder.documentName = rowView.findViewById(R.id.documentName);
documentViewHolder.documentAdd = rowView.findViewById(R.id.documentAdd);
documentViewHolder.documentUploadListView = rowView.findViewById(R.id.list_upload_document);
document.getDocumentUploadListAdapter().setDocumentUploadListClickInterface(this);
documentViewHolder.documentUploadListView.setAdapter(document.getDocumentUploadListAdapter());
rowView.setTag(documentViewHolder);
}
DocumentViewHolder documentViewHolder = (DocumentViewHolder) rowView.getTag();
documentViewHolder.documentName.setText(document.getDocumentName());
documentViewHolder.documentAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(documentListClickInterface!=null) {
documentListClickInterface.onDocumentListAddClick(document);
}
}
});
return rowView;
}
public void addDocument(Document document){
if(documentList==null){
documentList = new ArrayList<>();
}
documentList.add(document);
notifyDataSetChanged();
}
public List<Document> getDocumentList(){
return documentList;
}
@Override
public void onDocumentUploadListUpload(DocumentUploadListAdapter documentUploadListAdapter, DocumentUpload documentUpload) {
for(Document document: documentList){
if(document.getDocumentUploadListAdapter().equals(documentUploadListAdapter)){
documentListClickInterface.onDocumentUploadListUploadButton(document, documentUpload);
Log.d(TAG, "Added in: " + documentUploadListAdapter.getDocumentType());
break;
}
}
}
public class DocumentViewHolder{
public TextView documentName;
public Button documentAdd;
public ListView documentUploadListView;
}
public interface DocumentListClickInterface{
void onDocumentListAddClick(Document document);
void onDocumentUploadListUploadButton(Document document, DocumentUpload documentUpload);
}
}
添加到子级列表:
documentListAdapter - parent List
Document document = new Document();
document.setDocumentType(documentType);
document.setDocumentName(documentType.toString());
if(documentType.equals(DOCUMENT.Account_Statement)){
document.setMultiFile(true);
}
document.setDocumentUploadListAdapter(new DocumentUploadListAdapter());
document.getDocumentUploadListAdapter().setContext(getContext());
document.getDocumentUploadListAdapter().setDocumentType(documentType);
documentListAdapter.addDocument(document);
public void addDocumentUploadInDocumentList(File file){
if(currentDocument!=null){
DocumentUpload documentUpload = new DocumentUpload();
documentUpload.setFile(file);
documentUpload.setUploaded(false);
documentUpload.setFileName(file.getName());
currentDocument.getDocumentUploadListAdapter().addDocumentUpload(documentUpload);
currentDocument = null;
}
}