2017-06-15 28 views
0

コメントと説明付きの投稿があるアプリを作成しています。各投稿には3つのボタンがあります。 1つの説明2コメントと3番目はボタンのようです。カスタムアダプターの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

アダプターにクリックリスナーを実装し、ボタンにタグを設定するのが最も良い方法です。その後、ボタンのタグIDを取得してonclickメソッドで目的のタスクを実行すると、これは間違いなく機能します。

+0

お返事ありがとうございます。私は同じことをやっているが、同じ問題がまだ起きている.. –

+0

あなたのコードは同じものを示していない。あなたはsetonclicklistenerを直接見ています。 –

+0

今私はonClickListenerを別に実装していますが、同じ問題が発生しました....私を助けてくださいリスナーの上に –

関連する問題