OnClickListener无法使用片段

时间:2019-02-20 16:57:11

标签: android android-fragments onclicklistener

我无法让onclicklistenerfragments一起使用。我已经搜索了stackoverflow并尝试了所有技巧,但是我仍然无法使它正常工作。所以我在这里做我的第一篇文章。
我尝试将 android:focusable =“ false”,android:clickable =“ false” android:descendantFocusability =“ blocksDescendants” 添加到布局中没运气。我已经删除了它们,因为它们没有区别。我也尝试过其他解决方案,但没有一个可行。这是我的第一篇文章,因此,如果我发布了错误的内容让我知道并重做,我已经借了某人customlist来得到一个可行的示例。

这是我希望在

中单击列表的片段之一
public class ActivitiesFragment extends Fragment {
    ListView list;

    String[] maintitle ={
            "Aktivitet 1","Aktivitet 2",
            "Aktivitet 3","Aktivitet 4",
            "Aktivitet 5",
    };

    String[] subtitle ={
            "A","B",
            "C","D",
            "E",
    };

    Integer[] imgid={
            R.drawable.ic_dashboard_black_24dp,R.drawable.ic_dashboard_black_24dp,
            R.drawable.ic_dashboard_black_24dp,R.drawable.ic_dashboard_black_24dp,
            R.drawable.ic_dashboard_black_24dp,
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_activities, container,    false);

        MyListAdapter adapter = new MyListAdapter(getActivity(), maintitle, subtitle,imgid);

        list = (ListView) rootView.findViewById(R.id.list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 0) {
                    Toast.makeText(getActivity().getApplicationContext(),"One",Toast.LENGTH_SHORT).show();
                }
                else if(position == 1) {
                    Toast.makeText(getActivity().getApplicationContext(),"Two",Toast.LENGTH_SHORT).show();
                }
                else if(position == 2) {
                    Toast.makeText(getActivity().getApplicationContext(),"Three",Toast.LENGTH_SHORT).show();
                }
                else if(position == 3) {
                    Toast.makeText(getActivity().getApplicationContext(),"Four",Toast.LENGTH_SHORT).show();
                }
                else if(position == 4) {
                    Toast.makeText(getActivity().getApplicationContext(),"Five",Toast.LENGTH_SHORT).show();
                }
            }
        });

        return rootView;
    }
}

这是适配器:

public class MyListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] maintitle;
    private final String[] subtitle;
    private final Integer[] imgid;

    public MyListAdapter(Activity context, String[] maintitle,String[] subtitle, Integer[] imgid) {
        super(context, R.layout.mylist, maintitle);
        // TODO Auto-generated constructor stub

        this.context=context;
        this.maintitle=maintitle;
        this.subtitle=subtitle;
        this.imgid=imgid;
    }

    public View getView(int position,View rowView,ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.mylist, null,true);


        TextView titleText = (TextView) rowView.findViewById(R.id.title);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);

        titleText.setText(maintitle[position]);
        imageView.setImageResource(imgid[position]);
        subtitleText.setText(subtitle[position]);

        return rowView;
    }
}

该片段的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="50dp"/>
</RelativeLayout>

列表的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp"/>
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#4d4d4d" />
        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_marginLeft="10dp" />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

前段时间,我也遇到了类似的问题,列表项中包含ImageView。我的解决方案是在ImageView块中将[5, 3, "Code", 9, 4.5, "Pin"] 更改为false。我不知道为什么,但是效果很好。

无论如何,我强烈建议您开始使用android:focusableRecyclerViewhttps://developer.android.com/guide/topics/ui/layout/recyclerview

与ListView相比,它功能更强大,更灵活并且得到了重大增强。

答案 1 :(得分:0)

这里是RecyclerView的全部实现,带有项单击侦听器。

片段:

public class MyFragment extends Fragment implements ItemClickListener {

    RecyclerView rvList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_activities, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rvList = view.findViewById(R.id.rvList);

        ArrayList<ItemData> list = new ArrayList<>();
        list.add(new ItemData("Aktivitet 1","A",R.drawable.ic_dashboard_black_24dp))
        list.add(new ItemData("Aktivitet 2","B",R.drawable.ic_dashboard_black_24dp))
        list.add(new ItemData("Aktivitet 3","C",R.drawable.ic_dashboard_black_24dp))
        list.add(new ItemData("Aktivitet 4","D",R.drawable.ic_dashboard_black_24dp))
        list.add(new ItemData("Aktivitet 5","E",R.drawable.ic_dashboard_black_24dp))

        RVAdapter adapter = new RVAdapter(this, list);
        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        rvList.setAdapter(adapter);
    }

    @Override
    public void onItemClicked(ItemData data, int position) {
        // item click will be listened here
        Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
    }
}

片段布局:frag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

列表的布局:item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#4d4d4d"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

RecyclerView ViewHolder:

public class RecyclerVH extends RecyclerView.ViewHolder {

    private TextView titleText;
    private ImageView imageView;
    private TextView subtitleText;

    public RecyclerVH(@NonNull View itemView) {
        super(itemView);
        titleText = itemView.findViewById(R.id.title);
        imageView = itemView.findViewById(R.id.icon);
        subtitleText = itemView.findViewById(R.id.subtitle);
    }

    void bind(ItemData data) {
        titleText.setText(data.getMainTitle());
        imageView.setImageResource(data.getImgId());
        subtitleText.setText(data.getSubTitle());
    }
}

RecyclerView适配器:

public class RVAdapter extends RecyclerView.Adapter<RecyclerVH> {

    private ArrayList<ItemData> list;
    private ItemClickListener listener;

    public RVAdapter(ItemClickListener listener, ArrayList<ItemData> list) {
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RecyclerVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerVH holder, int position) {
        holder.bind(list.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClicked(list.get(holder.getAdapterPosition()),
                        holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}

interface ItemClickListener {
    void onItemClicked(ItemData data, int position);
}

更新1:

将这些行添加到项目布局的根标记中:

 android:focusable="true"
 android:clickable="true"

赞:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:clickable="true"
    android:orientation="horizontal">

    ...
</LinearLayout>