这是我的menu_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="118dp"
app:srcCompat="@drawable/three" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MENU"
android:textColor="@android:color/holo_red_dark"
android:textSize="22sp"
android:textStyle="bold|italic" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_weight="2"
/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="@android:color/holo_red_dark" />
</LinearLayout>
这是我的MenuFragment.java
package com.maharajarasoiandevents.maharajarasoiandevents.mFragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.maharajarasoiandevents.maharajarasoiandevents.ChaatActivity;
import com.maharajarasoiandevents.maharajarasoiandevents.ExoticSpecialityActivity;
import com.maharajarasoiandevents.maharajarasoiandevents.R;
import com.maharajarasoiandevents.maharajarasoiandevents.SaladsActivity;
import com.maharajarasoiandevents.maharajarasoiandevents.SoupActivity;
import com.maharajarasoiandevents.maharajarasoiandevents.StarterActivity;
import com.maharajarasoiandevents.maharajarasoiandevents.WelcomeDrinkActivity;
public class MenuFragment extends Fragment {
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View rootView=inflater.inflate(R.layout.menu_fragment,container,false);
listView = (ListView) rootView.findViewById(R.id.listView);
final String[] values = new String[]{"Welcome Drink","Moving Starter","Soup","Salads","Chaat","Exotic Speciality"};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(getActivity(), R.layout.menu_fragment, R.id.textView, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0) {
Intent myIntent = new Intent(rootView.getContext(), WelcomeDrinkActivity.class);
startActivityForResult(myIntent,0);
}
if(pos==1){
Intent myIntent=new Intent(rootView.getContext(), StarterActivity.class);
startActivityForResult(myIntent,1);
}
if(pos==2){
Intent myIntent=new Intent(rootView.getContext(), SoupActivity.class);
startActivityForResult(myIntent,2);
}
if(pos==3){
Intent myIntent=new Intent(rootView.getContext(), SaladsActivity.class);
startActivityForResult(myIntent,1);
}
if(pos==4){
Intent myIntent=new Intent(rootView.getContext(), ChaatActivity.class);
startActivityForResult(myIntent,2);
}
if(pos==5){
Intent myIntent=new Intent(rootView.getContext(), ExoticSpecialityActivity.class);
startActivityForResult(myIntent,1);
}
}
});
return rootView;
}
}
我只想要1个图像和1个textview,我在那个页面顶部写了MENU作为文本....甚至我在menu_fragment.xml中只拍了一张图像和1个textview ...但它显示了多个图像n textviews ...为什么这样?表示listview再次重复n但我不想每次都显示图像n textview ..就像listview ..我正在编辑我的代码..plz检查它:))
答案 0 :(得分:1)
试试这个:
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_fragment, container, false);
listView = (ListView) rootView.findViewById(R.id.listView);
final String[] values = new String[]{"starter", "juices", "roties"};
// edited
MyArrayAdapter adapter;
// edited ,add size and color parameters
adapter = new MyArrayAdapter(getActivity(), R.layout.menu_fragment, R.id.textView, values,Color.RED, 16f);
listView.setAdapter(adapter);
return rootView;
}
<强>被修改强>
添加MyArrayAdapter
public class MyArrayAdapter extends ArrayAdapter {
private int color;
private float size;
// edited ,add size and color parameters
public MyArrayAdapter(Context context, int layoutId, int resource, Object[] objects, int color, float size) {
super(context, layoutId, resource, objects);
this.color = color;
this.size = size;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String str = (String) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_fragment, parent, false);
}
TextView tv = (TextView) convertView.findViewById(R.id.textView);
tv.setText(str);
// edited , modified setTextSize and setTextColor
tv.setTextSize(size);
tv.setTextColor(color);
return convertView;
}
}
希望能帮到你。
答案 1 :(得分:0)
ArrayAdapter
类的构造函数将Context
作为第一个参数,而不是Fragment
。而不是this
使用getActivity
方法调用。
adapter = new ArrayAdapter<String>(getActivity(), R.layout.menu_fragment,R.id.textView,values);