如何在自定义ListView中获取当前行视图?

时间:2013-09-01 13:48:36

标签: android listview android-listview android-view

我有一个带有按钮的自定义列表视图,其中单击其中一个按钮( imgBtnSocial )会更改行内RelativeLayout的可见性。 我能够控制ListView中行的RelativeLayout可见性,但它不会定位当前单击的行。

这是我的适配器:

//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;
}

请帮助。

1 个答案:

答案 0 :(得分: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