2017-08-02 35 views
1

我有一个回收站视图列表。我正在显示另一个布局,点击它的父项目布局。如何显示列表中的一个项目的视图并隐藏其他项目。

现在,当我点击列表中的项目时,它显示了所有项目的视图,现在我想让布局只显示已点击的项目,并使其不再显示其他项目。

布局细节应该只在点击一个项目时才可见,并且只能被点击的项目。

布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 

    android:id="@+id/lay_row" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="false" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     xmlns:app="http://schemas.android.com/tools" 
     android:id="@+id/card_view" 
     app:cardUseCompatPadding="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="05dp" 
     android:layout_marginTop="05dp" 
     card_view:cardElevation="12dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:background="@color/bg"> 

      <View 
       android:layout_width="3dp" 
       android:layout_height="match_parent" 
       android:background="@color/cardLineColor"></View> 

      <LinearLayout 
       android:id="@+id/linearLayout6" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginEnd="10dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginStart="10dp" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/txt_id" 
        android:layout_width="103dp" 
        android:layout_height="wrap_content" 
        android:text="ID" 
        android:textColor="@color/white" /> 

       <TextView 
        android:id="@+id/txt_date" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="05dp" 
        android:text="23/3/2015" 
        android:textColor="@color/textColor" 
        android:textSize="10sp" /> 


      </LinearLayout> 

      <TextView 
       android:id="@+id/txt_trans_type" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignTop="@+id/linearLayout6" 
       android:layout_centerHorizontal="true" 
       android:text="sent money" 
       android:textColor="@color/white" 
       android:textStyle="bold" /> 

      <LinearLayout 
       android:id="@+id/linearLayout8" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignTop="@+id/linearLayout6" 
       android:layout_marginEnd="10dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginStart="10dp" 
       android:layout_toEndOf="@+id/txt_trans_type" 
       android:layout_toRightOf="@+id/txt_trans_type" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/txt_balance" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="end" 
        android:text="$3214" 
        android:textAlignment="viewEnd" 
        android:textColor="@color/white" 
        android:textStyle="bold" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="end" 
        android:layout_marginTop="05dp" 
        android:text="@string/balance" 
        android:textAlignment="viewEnd" 
        android:textColor="@color/white" 
        android:textSize="10sp" /> 


      </LinearLayout> 
     </RelativeLayout> 


    </android.support.v7.widget.CardView> 

    <RelativeLayout 
     android:id="@+id/relativeLayout_detail" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:visibility="gone"> 

     <RelativeLayout 
      android:id="@+id/relativeLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/trans_background"> 

      <View 
       android:id="@+id/view" 
       android:layout_width="2dp" 
       android:layout_height="match_parent" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_alignParentTop="true" 
       android:layout_marginBottom="2dp" 
       android:layout_marginTop="2dp" 
       android:background="@color/cardLineColor1"></View> 

      <LinearLayout 
       android:id="@+id/linearLayout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:layout_marginEnd="10dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginStart="10dp" 
       android:layout_toLeftOf="@+id/linearLayout86" 
       android:layout_toStartOf="@+id/linearLayout86" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/txt_vendor" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/vendor" 
        android:textColor="@color/lightGrey" /> 

       <TextView 
        android:id="@+id/txt" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="05dp" 
        android:text="@string/vendor" 
        android:textColor="@color/white" 
        android:textSize="10sp" /> 


      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/linearLayout86" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:layout_marginEnd="10dp" 
       android:layout_marginRight="10dp" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/txt_trans_id" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="ID" 
        android:textColor="@color/lightGrey" /> 

       <TextView 
        android:id="@+id/txt_7" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="05dp" 
        android:text="@string/transId" 
        android:textColor="@color/white" 
        android:textSize="10sp" /> 


      </LinearLayout> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/relativeLayout2" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/relativeLayout1" 
      android:background="@drawable/trans_background"> 

      <View 
       android:layout_width="2dp" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="2dp" 
       android:layout_marginTop="2dp" 
       android:background="@color/cardLineColor1"></View> 

      <LinearLayout 
       android:id="@+id/linearLayout9" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:layout_marginEnd="10dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginStart="10dp" 
       android:orientation="vertical"> 

       <TextView 
        android:id="@+id/txt_credit" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="$5858.0" 
        android:textColor="@color/lightGrey" /> 

       <TextView 
        android:id="@+id/txt5" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="05dp" 
        android:text="@string/credit" 
        android:textColor="@color/white" 
        android:textSize="10sp" /> 


      </LinearLayout> 
     </RelativeLayout> 
    </RelativeLayout> 
</LinearLayout> 

适配器:

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

    private List<Transaction> transactionList; 
    private Context mContext; 
    static final int TYPE_LOAD_TRANS = 0; 



    public TransactionHistoryListAdapter(List<Transaction> transactionsList, Context context) { 
     this.mContext = context; 
     this.transactionList = transactionsList; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     RecyclerView.ViewHolder viewHolder = null; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     switch (viewType) { 

      case TYPE_LOAD_TRANS: 
       View v_header = inflater.inflate(R.layout.transaction_item_layout, parent, false); 
       viewHolder = new TransactionHistoryListHolder(v_header); 
       break; 

     } 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

     if (getItemViewType(position) == TYPE_LOAD_TRANS) { 
      TransactionHistoryListHolder transsHolder = (TransactionHistoryListHolder) holder; 
      retriveAllTrans(transsHolder, position); 
     } else { 
     } 

    } 
    public void retriveAllTrans(final TransactionHistoryListHolder holder, final int position) { 


     final Transaction data = transactionList.get(position); 


     holder.txt_id.setText(data.getId()); 
     holder.txt_date.setText(data.getDate()); 
     holder.txt_trans_type.setText(data.getType()); 
     holder.txt_balance.setText(data.getBalance()); 


     holder.lay_row.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       holder.relativeLayout.setVisibility(View.VISIBLE); 

      } 
     }); 

    } 

    @Override 
    public int getItemViewType(int position) { 

     Transaction obj = transactionList.get(position); 

     if (obj.typeToDisp == 0) { 
      return TYPE_LOAD_TRANS; 
     } 
     return super.getItemViewType(position); 
    } 

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

,我们该怎么办呢?

编辑:

我试图做的是这样的:

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

    public TransactionHistoryListAdapter(List<Transaction> transactionsList, Context context) { 
     this.mContext = context; 
     this.transactionList = transactionsList; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     RecyclerView.ViewHolder viewHolder = null; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     switch (viewType) { 

      case TYPE_LOAD_TRANS: 
       View v_header = inflater.inflate(R.layout.transaction_item_layout, parent, false); 
       viewHolder = new TransactionHistoryListHolder(v_header); 
       break; 
     } 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
     final Transaction data = transactionList.get(position); 
     if (getItemViewType(position) == TYPE_LOAD_TRANS) { 


      TransactionHistoryListHolder transsHolder = (TransactionHistoryListHolder) holder; 

      if (data.isVisible()){ 
       transsHolder.relativeLayout.setVisibility(View.VISIBLE); 
      } else { 
       transsHolder.relativeLayout.setVisibility(View.GONE); 
      } 

      retriveAllTrans(transsHolder, position); 
     } else { 
     } 

    } 

    public void retriveAllTrans(final TransactionHistoryListHolder holder, final int position) { 


     final Transaction data = transactionList.get(position); 


     holder.txt_id.setText(data.getId()); 
     holder.txt_date.setText(data.getDate()); 
     holder.txt_trans_type.setText(data.getType()); 
     holder.txt_balance.setText(data.getBalance()); 


     holder.lay_row.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 



       if (data.isVisible()) { // initially it will be falsedata.setVisible(false); 

        data.setVisible(true); 
        holder.relativeLayout.setVisibility(View.VISIBLE); 
        notifyDataSetChanged(); 

       } else { 
        data.setVisible(false); 
        holder.relativeLayout.setVisibility(View.GONE); 
        notifyDataSetChanged(); 

       } 


      } 
     }); 


     for (int i=0;i< transactionList.size();i++) 
     { 
      if(i==position) { 
       data.setVisible(true); 
      } 
      else { 
       data.setVisible(false); 
      } 
     } 


    } 

    } 
    @Override 
    public int getItemViewType(int position) { 

     Transaction obj = transactionList.get(position); 

     if (obj.typeToDisp == 0) { 
      return TYPE_LOAD_TRANS; 
     } 

     return super.getItemViewType(position); 
    } 

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

它不工作了。

请帮忙。谢谢..

回答

0

在模型中维护标志以保持视图状态并只在条件满足您的模型时才显示。为了您在您的交易模型参考声明栏这样

public class Transaction { 
..... 
boolean isVisible; 

public void setIsVisible(boolean isVisible){ 
    this.isVisible = isVisible; 
    } 

public boolean isVisible(){ 
    return isVisible; 
} 
} 

然后在你的内心onClickListener适配器类设置的标志像这样

holder.lay_row.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (data.isVisble()) { // initially it will be false 
      data.setIsVisible(false); 
      holder.relativeLayout.setVisibility(View.GONE); 
      } else { 
      data.setIsVisible(true); 
      holder.relativeLayout.setVisibility(View.Visible); 
      } 

     } 
    }); 

这将显示你的观点,但在滚动的意见将被回收和设置为默认状态,所以要解决这个问题,你必须更新内部onBindView

您的视图状态
@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 
    final Transaction data = transactionList.get(position); 
    if (getItemViewType(position) == TYPE_LOAD_TRANS) { 
     if (data.isVisible()){ 
     holder.relativeLayout.setVisibility(View.VISIBLE); 
     } else { 
     holder.relativeLayout.setVisibility(View.GONE); 
     } 
     TransactionHistoryListHolder transsHolder = (TransactionHistoryListHolder) holder; 
     retriveAllTrans(transsHolder, position); 
    } else { 
    } 

} 

这将解决您的问题

+0

它不能正常工作我可以看到每个项目的布局,对于其他项目它不会隐藏。 – Sid

+0

你好,你在吗? @sohan shetty – Sid

+0

你有多个视图类型? –

相关问题