如何在Fragment中实现onClick?

时间:2016-10-31 11:29:59

标签: java android listview

final ArrayList<User> allBoxerData= new ArrayList<User>();

        allBoxerData.add(new User("Ramon \nDekkers",R.drawable.ramonfinalmej));
        allBoxerData.add(new User("Sergio \nMartínez",R.drawable.sergiomej1));
        allBoxerData.add(new User("Kiko \nMartínez",R.drawable.kikomej1));
        allBoxerData.add(new User("Roberto \nSantos",R.drawable.robertofinal));
        allBoxerData.add(new User("Dani \nCuevas",R.drawable.danimej1));
        allBoxerData.add(new User("Roman \nVardanyan",R.drawable.romanfinal));
        allBoxerData.add(new User("Ivan \nSalcines",R.drawable.ivanred));
        allBoxerData.add(new User("Eva María \nNaranjo",R.drawable.evacuadrado));
        allBoxerData.add(new User("Manzy \nPauwells",R.drawable.manzymej1));
        allBoxerData.add(new User("Lara \nMartín",R.drawable.laramej1));
        allBoxerData.add(new User("Antonio \nRodríguez",R.drawable.antoniomej));
        allBoxerData.add(new User("Katharina \nThanderz",R.drawable.katmej1));


        mylist = (ListView) view.findViewById(R.id.mylist);

        usersAdapter = new UsersAdapter(getActivity(),allBoxerData);


        mylist.setAdapter(usersAdapter);
        usersAdapter.notifyDataSetChanged();

        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
            //Log.i("List View Clicked", "**********");
            if (position==1) {
                Toast.makeText(getActivity(), "Listttt View Clicked:" + position, Toast.LENGTH_LONG).show();
            }
        }
    });

有什么问题?我的申请没有向我显示Toast。此代码位于extends Fragment,我有一个listview。我需要点击listview中的1个项目,然后转到其他Fragment,但这不起作用。

public class UsersAdapter extends BaseAdapter {


    private static LayoutInflater inflater = null;
    public List<User> allUsers;

    private Context context;

    /*************
     * Constructor
     *****************/
    public UsersAdapter(Context ctx, ArrayList<User> data) {


        context = ctx;
        allUsers = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public int getCount() {
        return allUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        UserViewHolder holder = new UserViewHolder();

        final User user = allUsers.get(position);


        if(convertView==null)
        {
            rowView = inflater.inflate(R.layout.boxer_item, null);
            holder.name = (TextView) rowView.findViewById(R.id.txt_name);
            holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

            rowView.setTag(holder);
        }
        else
        {
            holder = (UserViewHolder) rowView.getTag();
        }




        holder.name.setText(user.getName());

        Glide.with(context).load(user.getImage()).into(holder.icon);


        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

               if (user.getName().equals("Ramon \nDekkers")) {


                    Toast.makeText(getActivity(), "Listttt View Clicked:" + position, Toast.LENGTH_LONG).show(); // THIS WORKS BUT I NEED GO TO OTHER FRAGMENT. 

               }
            }
        });
        return rowView;
    }


    private static class UserViewHolder {
        public ImageView icon;
        public TextView name;

    }
}

3 个答案:

答案 0 :(得分:1)

像这样更改你的上下文。

private AppCompatActivity context;

更改您的UsersAdapter构造函数

public UsersAdapter(AppCompatActivity ctx, ArrayList<User> data) {
  //Your code
}

然后在你的点击监听器中添加代码以转到下一个片段

rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
          //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();
        Fragment fragment = new NextFragment();
        FragmentManager fragmentManager = context.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.your_content_frame, fragment);
        fragmentTransaction.addToBackStack(null); //If you want to add to back stack
        fragmentTransaction.commit();
        }
    });

答案 1 :(得分:0)

试试这个:

rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

           Fragment fragment = new NextFragment();
           FragmentTransaction fragmentTransaction = getfragmentManager().beginTransaction();
           fragmentTransaction.replace(/*This needs to bee your fragment container*/, fragment);
           fragmentTransaction.addToBackStack(null);
           fragmentTransaction.commit();
        }
     }
 });

答案 2 :(得分:0)

要调用mylist.setOnItemClickListener(new AdapterView.OnItemClickListener(),您必须删除该点击侦听器。

rowView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
      //  Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

       if (user.getName().equals("Ramon \nDekkers")) {


            Toast.makeText(getActivity(), "Listttt View Clicked:" + position, Toast.LENGTH_LONG).show(); // THIS WORKS BUT I NEED GO TO OTHER FRAGMENT. 

       }
    }
});

如果您有RowClickListener,则会在那里处理触摸,而不是ItemClickListener

<强>更新
并从你需要的片段中打开一个片段

rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

           Fragment fragment = new NextFragment();
           FragmentManager fragmentManager = context.getFragmentManager();
           FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
           fragmentTransaction.replace(R.id.your_content_frame, fragment);
           fragmentTransaction.addToBackStack(null); //If you want to add to back stack
           fragmentTransaction.commit();
        }
     }
 });

当您需要获得支持片段管理器调用时:

FragmentManager fragManager = myContext.getSupportFragmentManager(); //If using fragments from support v4

FragmentManager fragManager = myContext.getFragmentManager();

你已经完成了。