2017-06-15 37 views
0

我正在製作一個應用程序,其中有包含註釋和說明的帖子。每個帖子上有三個按鈕。 1個描述2個評論和第三個就像按鈕。我在自定義適配器的getview方法中設置了一個按鈕單擊偵聽器。當我點擊描述按鈕時,該帖子的描述應該顯示在按鈕下方。但是當我點擊描述按鈕時,其他一些列表視圖項目的描述也顯示出來了。我只想顯示描述按鈕被點擊的帖子的描述。這裏是我的代碼:單擊列表視圖項目中的按鈕時,也會在其他一些列表視圖項目中發生更改

getview代碼:

public View getView(int position, View convertView, ViewGroup parent) { 
     a = getItem(position); 

     View view = convertView; 
     try 
     { 
      if (convertView == null) { 
       v = new ViewHolder(); 
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false); 
       v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image); 
       v.desc = (Button) convertView.findViewById(R.id.btn_desc); 
       v.des = (TextView) convertView.findViewById(R.id.tv_list_desc); 
       v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc); 


       convertView.setTag(v); 
      } 
      else { 
       v = (ViewHolder) convertView.getTag(); 
      } 
      v.desc.setTag(position); 
      //Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView); 
      Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring).into(v.imgView); 

      v.desc.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v1) { 
        Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show(); 
        v.des.setText(""+a.getDescription()); 
        v.ll.setVisibility(View.VISIBLE); 
       } 
      }); 


      return convertView; 

     }catch (Exception e) 
     { 
      return null; 
     } 

    } 

XML代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="35dp"> 
    <ImageView 
     android:id="@+id/iv_list_image" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" /> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_weight="6"> 
     <Button 
      android:id="@+id/btn_desc" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:text="Description" 
      /> 
     <Button 
      android:id="@+id/btn_comment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:text="Comments" 
      /> 
     <Button 
      android:id="@+id/btn_likes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="2" 
      android:text="Likes" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/ll_desc" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="16dp" 
     android:background="#ffffff" 
     android:visibility="invisible" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Description:" 
      android:textStyle="bold"/> 
     <TextView 
      android:id="@+id/tv_list_desc" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Default Desc" 
      android:textColor="#333" 
      /> 
    </LinearLayout> 
</LinearLayout> 

回答

0

當你使用一個int標誌列表項的立場,即是作爲更新ListView項目之前的條件語句。

所以你的情況下,適配器類將是這個樣子:

... 

private int mPosition = -1; // Int flag that doesn't take effect UNTIL it has been set 

... 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    a = getItem(position); 

    View view = convertView; 
    try { 
     if (convertView == null) { 
      v = new ViewHolder(); 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false); 
      v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image); 
      v.desc = (Button) convertView.findViewById(R.id.btn_desc); 
      v.des = (TextView) convertView.findViewById(R.id.tv_list_desc); 
      v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc); 

      convertView.setTag(v); 
     } else { 
      v = (ViewHolder) convertView.getTag(); 
     } 

     v.desc.setTag(position); 

     //Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView); 
     Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring) 
       .into(v.imgView); 

     // Runs the following functionality if the positions match. Otherwise, hides the layout. 
     if (mPosition == position) { 
      Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show(); 
      v.des.setText(""+a.getDescription()); 
      v.ll.setVisibility(View.VISIBLE); 
     } else { 
      v.ll.setVisibility(View.INVISIBLE); 
     } 

     v.desc.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v1) { 
       mPosition = position; // Sets the flag to the position that was clicked 

       notifyDataSetChanged(); // Updates the data instantly 
      } 
     }); 

     return convertView; 

    } catch (Exception e) { 
     return null; 
    } 
} 

讓我知道是否可行。

+0

setonitemclicklistener不在這裏工作... –

+0

@MuhammadAbdullah我更新了答案。試試看:-) – DaveNOTDavid

+1

非常感謝。它的工作現在。你節省了我的一天。再次感謝。 –

0

最好的方法是在適配器上實現點擊監聽器,並在按鈕上設置標籤。之後,在onclick方法中,通過獲取按鈕的標籤ID來執行所需的任務,這一定會起作用。

+0

感謝您的答覆。我正在做同樣的事情,但仍然得到相同的問題。 –

+0

您的代碼doesnot顯示相同。您直接在視圖中執行setonclicklistener。 –

+0

現在我實現onClicklistener單獨但同樣的問題....請幫我出 –