列出具有Navigationdrawer活动的片段,itemSelected

时间:2017-08-25 11:23:25

标签: android listview fragment navigation-drawer

我有一个应用程序,带有导航抽屉活动,主要活动是空的,在片段中我有一个带图像的列表。布局是:

    <FrameLayout 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="com.example.iblancogonzalez.innch.Cocktail">
<LinearLayout
          android:orientation="horizontal"
    android:layout_width="fill_parent"
     android:layout_height="wrap_content"
android:background="@drawable/post_border_style"
        >

<ImageView
    android:id="@+id/icon"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:padding="5dp" />
    <LinearLayout android:layout_width="fill_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center" >

 <TextView
        android:id="@+id/texto_principal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:padding="2dp"
        android:textColor="#ff020f" />
        <TextView
        android:id="@+id/texto_secundario"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginLeft="10dp"
        android:textColor="#0db112" />
</LinearLayout>
</LinearLayout>
    <ListView
    android:id="@+id/mi_lista"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

全部在片段中。

然后我有一个构造函数:

    public class AdaptadorCocktail extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;

private final Integer[] integers;


public AdaptadorCocktail(Activity context, String[] itemname, Integer[] integers) {
    super(context, R.layout.fragment_cocktail, itemname);

    this.context=context;
    this.itemname=itemname;
    //this.itemexpl=itemexpl;
    this.integers=integers;
}


public View getView(int posicion,View view, ViewGroup parent){

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //LayoutInflater inflater=context.getLayoutInflater();

    View rowView=inflater.inflate(R.layout.fragment_cocktail,null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.texto_principal);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView etxDescripcion = (TextView) rowView.findViewById(R.id.texto_secundario);

    txtTitle.setText(itemname[posicion]);
    imageView.setImageResource(integers[posicion]);
            return rowView; }}

然后在我的片段中我有:

    public class Cocktail extends Fragment {
private String lenguajeProgramacion[]=new String[]{"Bolum Crujiente de morcilla, queso, manzana y piña","Bolum de foie, nuees y gelatina de vino tinto","Bolum de foie con almendra caramelizada y mango","Chulipop de queso Idiazabal","Chulipop de salmon con queso manchego","Chulipop de foie con chocolate","Macarron de foie"};
private String itemexplc[]=new String[]{"(Formato 14g, 30 unidades por estuche, caja de 6 estuches)","(Formato 14g, 30 unidades por estuche, caja de 6 estuches)","(Formato 14g, 30 unidades por estuche, caja de 6 estuches)","(Formato 15g, 40 unidades por estuche, caja de 6 estuches)","(Formato 15g, 40 unidades por estuche, caja de 6 estuches)","(Formato 15g, 40 unidades por estuche, caja de 6 estuches)","(Formato 10g, 48 unidades por estuche, caja de 6 estuches)"};

View rootView;
private Integer[] imgid={R.mipmap.bola,
        R.mipmap.bolb,R.mipmap.bolc,R.mipmap.chula,R.mipmap.chulb,R.mipmap.chulc,R.mipmap.maca};
private ListView lista;


// 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 OnFragmentInteractionListener mListener;

public Cocktail() {

    // 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 Cocktail.
 */
// TODO: Rename and change types and number of parameters
public static Cocktail newInstance(String param1, String param2) {
    Cocktail fragment = new Cocktail();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}



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


    // Inflate the layout for this fragment

    rootView = inflater.inflate(R.layout.fragment_cocktail, container, false);

   AdaptadorCocktail adapter = new AdaptadorCocktail(getActivity(),lenguajeProgramacion,imgid);

    lista=(ListView)rootView.findViewById(R.id.mi_lista);
    lista.setAdapter(adapter);
    Toast.makeText( getActivity(),"ojo1", Toast.LENGTH_SHORT).show();
    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String Slecteditem= lenguajeProgramacion[+position];
            Toast.makeText( getApplicationContext(),Slecteditem, Toast.LENGTH_SHORT).show();
        }
    });
    return rootView;}







    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;
}



/**
 * 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);
}
}

我有另一个但是工作得非常好,但是使用rootView并且主要活动定义了参数。 所有工作都非常好,但当我点击列表时,它不起作用。 ***GetaplicationContext()***它是红色的,这是一个错误。我尝试使用 getActivity (),***和getActivityGetapplicationContet(),***错误消失,但它不起作用。 我认为rootView就是问题所在。 一些错误或其他类型的代码的想法吗? 谢谢大家。

1 个答案:

答案 0 :(得分:0)

在项目上实现点击侦听器而不是listeview。在Adapter类的getView()方法中实现它,如下所示 -

public View getView(int posicion,View view, ViewGroup parent){

LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//LayoutInflater inflater=context.getLayoutInflater();

final View rowView=inflater.inflate(R.layout.fragment_cocktail,null,true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.texto_principal);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView etxDescripcion = (TextView) rowView.findViewById(R.id.texto_secundario);

txtTitle.setText(itemname[posicion]);
imageView.setImageResource(integers[posicion]);
rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //put message here
            String Slecteditem= lenguajeProgramacion[+position];
            Toast.makeText( context, Slecteditem, Toast.LENGTH_SHORT).show();}
            return rowView; }}
}
相关问题