如何在列表适配器中获取按钮的父视图?

0 android view parent rootview

我来自巴西,我有一个包含按钮图像和文本的列表视图.我正在尝试点击按钮列表视图并获取负责我点击的行的视图.所以我可以在我的按钮所在的行上进行更改.

发生了什么,如果下面的代码,当我点击按钮时,列表中的任何按钮他总是在第一个列表项中进行更改,我希望他在各自的行中执行.因此这个清单

IMAGE ------------ ---------------文本 - 按钮
图像------------ ----- ----------文字 - 按钮
图像------------ ---------------文字 - 按钮
图像--- --------- ---------------文本 - 按钮

我点击按钮,他只是更改了他的行示例的文本:

IMAGE ------------ ---------------文本 - 按钮
图像------------ ----- ---------改变 - 按钮< - 点击
图像------------ ---------------文本 - 按钮
图像------------ ---------------文本 - 按钮

和..

IMAGE ------------ ---------------文本 - 按钮
图像------------ ----- ----------文字 - 按钮
图像------------ ---------------文字 - 按钮
图像--- --------- --------------更改 - 按钮< - 点击

回想一下,并在片段列表中使用了一个适配器抱歉,我的英语很糟糕!

public class JAdapterList extends BaseAdapter  implements OnClickListener {

    private LayoutInflater mInflater;
    private ArrayList<JItemList> itens;

    public JAdapterList(Context context, ArrayList<JItemList> itens) {
        //Itens que preencheram o listview
        this.itens = itens;
        //responsavel por pegar o Layout do item.
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return itens.size();
    }

    public JItemList getItem(int position) {
        return itens.get(position);
    }

        public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View view, ViewGroup parent) {
        //Pega o item de acordo com a posção.
        JItemList item = itens.get(position);
        //infla o layout para podermos preencher os dados

        view = mInflater.inflate(R.layout.navlistitem, null);

        ((TextView) view.findViewById(R.id.textListView)).setText(item.getNome());


        ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton1);
        ib.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub


    View vv = v.getRootView();


    ImageView tv = (ImageView) vv.findViewById(R.id.imageView1);
    TextView texto = (TextView) vv.findViewById(R.id.textListView4);
    texto.setText("CHANGE");
    tv.setAnimation(AnimationUtils.loadAnimation(tv.getContext(), R.anim.motion));




    }
}
Run Code Online (Sandbox Code Playgroud)

Ray*_*Ray 5

您可以将文本视图设置为按钮的标记,这样可以更轻松.像这样:在getView中:

TextView textView = ((TextView)  view.findViewById(R.id.textListView)).setText(item.getNome());


    ImageButton ib = (ImageButton) view.findViewById(R.id.imageButton1);
    ib.setOnClickListener(this);
     ib.setTag(textView);
Run Code Online (Sandbox Code Playgroud)

在onClickListener中:

 public void onClick(View v) {
    TextView texto = (TextView) v.getTag();
    texto.setText("CHANGE");
  }
Run Code Online (Sandbox Code Playgroud)