OnClickListener不适用于textview

时间:2017-03-31 09:51:34

标签: android

我正在尝试按下textView时启动一个新片段。我尝试了其他方法,但onClickListener仍然无法正常工作。我没有崩溃,没有。我也尝试过使用按钮,或者在OncreateView方法中创建onClick方法并设置一个监听器。

Fragment:package georgia.languagelandscape.fragments;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import georgia.languagelandscape.R;

//import android.support.v4.app.Fragment;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ProfileFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ProfileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HelpFragment extends Fragment implements MyProjectsFragment.OnFragmentInteractionListener, View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private FragmentManager fm = null;
    private FragmentTransaction ft = null;

    private OnFragmentInteractionListener mListener;

    public HelpFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ProfileFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HelpFragment newInstance(String param1, String param2) {
        HelpFragment fragment = new HelpFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    private ListView mListView;

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

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
     //   String frameVideo = "<html><body>Youtube video .. <br> <iframe width=\"320\" height=\"315\" src=\"https://www.youtube.com/embed/lY2H2ZP56K4\" frameborder=\"0\" allowfullscreen></iframe></body></html>";

        View rootview = inflater.inflate(R.layout.fragment_help, container, false);

        TextView tv= (TextView) rootview.findViewById(R.id.item1);
        tv.setFocusableInTouchMode(false);
        tv.setOnClickListener(this);


    /*   WebView display = (WebView) rootview.findViewById(R.id.webView);
        display.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });
        WebSettings webSettings = display.getSettings();
        webSettings.setJavaScriptEnabled(true);
        display.loadData(frameVideo, "text/html", "utf-8");*/




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



    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

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

    @Override
    public void onClick(View v) {

            switch (v.getId()) {
                case R.id.item1:
                    MyProjectsFragment myProjectsFragment= new MyProjectsFragment();
                    fm = getFragmentManager();
                    ft = fm.beginTransaction();
                    Log.d("dcf","da");
                    ft.replace(R.id.content_replace, myProjectsFragment);
                    ft.commit();
                    break;
                /*case R.id.item2:
                    //Do what you want for create_button
                    break;
                default:
                    break;*/
            }


    }


    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
    }


}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="georgia.languagelandscape.fragments.HelpFragment"
    android:background="@android:color/background_light"
    tools:background="@android:color/white">



            <TextView
                android:text="Help"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="17dp"
                android:id="@+id/textView"
                android:textSize="30sp"
                android:textStyle="normal|bold"
                android:textColor="#000"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"/>
    <TextView
        android:id="@+id/item1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="50dp"
        android:text="item1"
        android:clickable="true" />


            <WebView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/webView"
                android:layout_marginTop="100dp"
                android:padding="10dp"
                android:layout_below="@+id/textView"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:4)

您有一个名为rootview的视图。当你最后return inflater.inflate(R.layout.fragment_help, container, false);时,你正在返回另一个视图。

替换:

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

使用:

return rootview;

就是这样。