2013-09-01 68 views
1

我有一個自定義ListView的按鈕,其中按鈕之一(imgBtnSocial)單擊時會更改該行內的RelativeLayout的可見性。 我可以控制ListView中行的RelativeLayout可見性,但它不會鎖定當前點擊行。如何獲取自定義ListView中的當前行視圖?

這裏是我的適配器:

//Main Listview Adapter 

public class EfficientAdapter extends BaseAdapter { 
static FragmentActivity act; 

public ArrayList<Post> data; 
static LayoutInflater inflater = null; 
public static ViewHolder holder; 

EfficientAdapter(FragmentActivity a, ArrayList<Post> d) { 

    act = a; 
    data = d; 
    inflater = (LayoutInflater) act 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 



} 

public int getCount() { 
    return data.toArray().length; 

} 

public Object getItem(int position) { 

    return position; 
} 

public long getItemId(int position) { 

    return position; 
} 

@Override 
public int getItemViewType(int position) { 
    return (position == 0) ? 1 : 0; 
} 

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

class ViewHolder { 
    public TextView label; 
    public TextView body; 
    public ImageButton imgBtnComment; 
    public ImageButton imgBtnStar; 
    public ImageButton imgBtnShare; 
    ImageButton imgBtnSocial; 
    RelativeLayout socialBar_rl; 


    public int position; 



} 

View vi; 
static int pos; 
int counter; 
DisplayImageOptions options; 

int viewMode; 

public View getView(int position, View convertView, final ViewGroup parent) { 
    vi = convertView; 

    int theType = getItemViewType(position); 

    if (vi == null) { 
     holder = new ViewHolder(); 

       vi = inflater.inflate(R.layout.row_card, null); 
       holder.label = (TextView) vi.findViewById(R.id.title); 
       holder.body = (TextView) vi.findViewById(R.id.body); 

       holder.imgBtnComment = (ImageButton) vi 
         .findViewById(R.id.imageButton1); 
       holder.imgBtnComment.setFocusable(false); 

       holder.imgBtnStar = (ImageButton) vi 
         .findViewById(R.id.imageButton3); 
       holder.imgBtnStar.setFocusable(false); 

       holder.imgBtnSocial = (ImageButton) vi 
         .findViewById(R.id.imageButton4); 
       holder.imgBtnSocial.setFocusable(false); 

       holder.socialBar_rl = (RelativeLayout) vi 
         .findViewById(R.id.newsSocialPopup); 

       holder.imgBtnShare = (ImageButton) vi 
         .findViewById(R.id.imageButton2); 
       holder.imgBtnShare.setFocusable(false); 

     vi.setTag(holder); 

    } else 
     holder = (ViewHolder) vi.getTag(); 


      holder.position = position; 
      String title = data.get(position).getTitle(); 
      String body = data.get(position).getListDescription(); 
      holder.label.setText(title); 

      holder.body.setText(body); 

      // Comments Button 
      holder.imgBtnComment.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 

        FragmentViewHome homeFragment = (FragmentViewHome) act 
          .getSupportFragmentManager().findFragmentByTag(
            "homelist"); 

        final int position = homeFragment.listview_t 
          .getPositionForView((LinearLayout) v 
            .getParent()) - 2; 

        Bundle bundle = new Bundle(); 
        bundle.putString("title", data.get(position).getTitle()); 
        bundle.putInt("position", position); 
        bundle.putString("entity", data.get(position).getUrl()); 
        Intent intent = new Intent(act, 
          CommentActivity.class); 
        intent.putExtras(bundle); 
        act.startActivity(intent); 

       } 
      }); 

      // Comments Button 
      holder.imgBtnShare.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        FragmentViewHome homeFragment = (FragmentViewHome) act 
          .getSupportFragmentManager().findFragmentByTag(
            "homelist"); 

        final int position = homeFragment.listview_t 
          .getPositionForView((LinearLayout) v 
            .getParent()) - 2; 

        Intent shareIntent = new Intent(
          android.content.Intent.ACTION_SEND); 

        // set the type 
        shareIntent.setType("text/plain"); 

        // add a subject 
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
          data.get(position).getUrl()); 
        act.startActivity(Intent.createChooser(
          shareIntent, "Compartir en:")); 

       } 
      }); 

      holder.imgBtnStar.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 

        new createbookmark(v).execute(); 

       } 
      }); 

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

         public void onClick(View v) { 
          // TODO Auto-generated method stub 

          FragmentViewHome homeFragment = (FragmentViewHome) act 
            .getSupportFragmentManager() 
            .findFragmentByTag("homelist"); 

          final int position = homeFragment.listview_t 
            .getPositionForView((LinearLayout) v 
              .getParent()) - 2; 
          View view = parent.getChildAt(position); 

          RelativeLayout socialrl = (RelativeLayout) view 
            .findViewById(R.id.newsSocialPopup); 

          if (socialrl.getVisibility() == View.GONE) { 
           Animation anim_social = AnimationUtils 
             .loadAnimation(
               act, 
               R.anim.socialbar_slide_in_left); 

           socialrl.startAnimation(anim_social); 

           socialrl.setVisibility(holder.socialBar_rl.VISIBLE); 
          } else if (socialrl.getVisibility() == View.VISIBLE) { 

           Animation anim_social = AnimationUtils 
             .loadAnimation(
               act, 
               R.anim.socialbar_slide_out_right); 

           socialrl.startAnimation(anim_social); 
           socialrl.setVisibility(socialrl.GONE); 

          } 
         } 

        }); 



    return vi; 
} 

請需要幫助。

回答

2

我現在工作了。 這裏是任何有興趣的代碼:

holder1.imgBtnSocial 
        .setOnClickListener(new View.OnClickListener() { 

         public void onClick(View v) { 
          // TODO Auto-generated method stub 

          FragmentViewHome homeFragment = (FragmentViewHome) activityf 
            .getSupportFragmentManager() 
            .findFragmentByTag("homelist"); 
          // current position or whatever position you're looking for 
          final int currentPosition = homeFragment.listview_t 
            .getPositionForView((LinearLayout) v 
              .getParent()) - 2; 
          //firstPosition in listview 
          int firstPosition = homeFragment.listview_t 
            .getFirstVisiblePosition() 
            - homeFragment.listview_t 
              .getHeaderViewsCount(); 
          //get the position of te child 
          int wantedChild = currentPosition 
            - firstPosition; 
          View wantedView = homeFragment.listview_t 
            .getChildAt(wantedChild); 

          RelativeLayout socialrl = (RelativeLayout) wantedView 
            .findViewById(R.id.newsSocialPopup); 

          if (socialrl.getVisibility() == View.GONE) { 
           Animation anim_social = AnimationUtils 
             .loadAnimation(
               activityf, 
               R.anim.socialbar_slide_in_left); 

           socialrl.startAnimation(anim_social); 

           socialrl.setVisibility(socialrl.VISIBLE); 
          } else if (socialrl.getVisibility() == View.VISIBLE) { 

           Animation anim_social = AnimationUtils 
             .loadAnimation(
               activityf, 
               R.anim.socialbar_slide_out_right); 

           socialrl.startAnimation(anim_social); 
           socialrl.setVisibility(socialrl.GONE); 

          } 
         } 

        }); 

希望它可以幫助任何人。 :D

+0

雅+2兩個..保持它的兄弟。 – Ranjit

相關問題