【问题标题】:Button onclick inside fragment is not working片段内的按钮 onclick 不起作用
【发布时间】:2014-06-22 20:11:36
【问题描述】:

我正在尝试为我的一个视图(片段内)内的按钮(R.id.buy_button)设置/覆盖onclick 方法。应用程序不返回错误。但是它不会触发 onclick 方法。我不确定我的实现是否错误,或者我的按钮配置不正确。任何帮助/cmets/指导将不胜感激。

我的 onCreateView 在这里:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.list_item, container, false);
    Button b = (Button) view.findViewById(R.id.buy_button);
    b.setOnClickListener(this);

    return inflater.inflate(R.layout.fragment_item_list, container, false);
}

我的整个片段代码:

public static class AlbumsFragment extends ListFragment implements View.OnClickListener {
        private static final String ARG_SECTION_NUMBER = "section_number";

        public AlbumsFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle b){
            super.onActivityCreated(b);

            ListView listView = getListView();
            List<Album> albums = null;

            albums = Album.listAll(Album.class);
            Log.i("ALBUMS", albums.toString());
            ArrayAdapter<Album> adapter =
                    new ArrayAdapter<Album>(getActivity(), R.layout.list_item, R.id.textView,  albums);
            if (!albums.equals("")) {
                listView.setAdapter(adapter);
            }
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }

        @Override
        public void onDetach() {
            super.onDetach();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.list_item, container, false);
            Button b = (Button) view.findViewById(R.id.buy_button);
            b.setOnClickListener(this);

            return inflater.inflate(R.layout.fragment_item_list, container, false);
        }

        public void gotoPurchaseWeb() {
            Context context = getActivity().getApplicationContext();
            Intent intent = new Intent(context, PurchaseSong.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

        public static AlbumsFragment newInstance(int sectionNumber) {
            AlbumsFragment fragment = new AlbumsFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.buy_button:
                    Context context = getActivity().getApplicationContext();
                    Intent intent = new Intent(context, PurchaseSong.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    break;
            }
        }
    }

我的 list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:weightSum="1"
        android:showDividers="middle|end"
        android:visibility="visible">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/black"/>

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/textView"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:layout_marginTop="@dimen/zero_space"
            android:layout_marginBottom="@dimen/zero_space"
            android:paddingTop="3dp"
            android:paddingLeft="8dp"
            android:paddingRight="5dp"
            android:paddingBottom="0dp"
            android:layout_weight="24.33" />

    </LinearLayout>

    <Button
        style="@style/ButtonText"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/button_height"
        android:text="@string/buy_album"
        android:id="@+id/buy_button"
        android:background="@drawable/blue_button"
        android:paddingTop="7dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp" />

我的整个项目: https://github.com/markbratanov/MyFitPlayer/tree/master/Player/src/main/java/com/markbratanov/myfitplayer

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: android android-layout android-fragments android-view android-button


    【解决方案1】:

    您应该在 Fragment 内部的 onCreateView 回调函数中返回您的视图,而不是返回您的视图的新膨胀实例。

    return inflater.inflate(R.layout.fragment_item_list, container, false);
    

    改为使用:

    return view;
    

    【讨论】:

    • 是的,创建视图会在没有为按钮分配 onClick 侦听器的情况下重新生成视图
    • @user3455363 :绝对
    • @Arash 这是有道理的。试了一下,返回 NullPointerException。该按钮位于与片段视图 (fragment_item_list) 不同的布局文件 (list_item) 中。如何加载两者?
    • @markbratanov :如果您的按钮在主布局内,那么您应该使用主 Activity 上下文设置该按钮的侦听器。您可以在 Fragment 中使用 getActivity() 访问您的 MainActivity 上下文.所以你可以使用 Button b = (Button)getActivity().findViewById(R.id.buy_button);
    • 我花了一个小时来解决这个部分问题。谢谢。
    【解决方案2】:

    我遇到了同样的问题。我的按钮 onclick 侦听器在片段中不起作用。

    试试这个:在关闭 TAG 之前的按钮 XML 中添加 android:onclick="" 并在 mainActivity.java 文件中创建一个与您在其中声明的类名称相同的类XML。然后在onclick 事件中做任何你想做的事情。将其写入您最近为单击事件创建的类中。

    【讨论】:

      【解决方案3】:

      在 onCreateView 视图方法中执行此操作,您需要像这样为按钮单击充气片段

        View view = inflater.inflate(R.layout.fragment_blank3,
                  container, false);
         Button bt1=(Button)view.findViewbyId(R.id.buttton);
         bt1.setOnclick...
         //then return in on create view.
          return view;
      

      【讨论】:

        猜你喜欢
        • 2018-10-08
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-10
        • 1970-01-01
        相关资源
        最近更新 更多