我在tabhost中有一个 ListView 。一切正常,但 ListView 无法正常滚动。
这是我的XML布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/liner"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:id="@+id/listView11"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
这是JAVA课程:
public class NotFragment extends Fragment {
MyCustomAdapter myAdapter = new MyCustomAdapter();
ListView listView;
public static final String SecimId = "planet_number";
public NotFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.yemeklistesi, container, false);
int secim = getArguments().getInt(SecimId);
String Title = Gunler[secim];
// get the listview
TabHost mytabhost =(TabHost) rootView.findViewById(R.id.tabhost);
mytabhost.setup();
//listView = (ListView)rootView.findViewById(R.id.listInclude);
TabSpec spec;
spec = mytabhost.newTabSpec("yemekhane");
spec.setContent(R.id.liner);
spec.setIndicator("Yemekhane");
listView = (ListView)findViewById(R.id.listView11);
prepareData("Yemekhane");
mytabhost.addTab(spec);
mytabhost.addTab(mytabhost.newTabSpec("sosyaltesisler").setIndicator("Sosyal Tesisler").setContent(R.id.liner));
mytabhost.addTab(mytabhost.newTabSpec("turuncusalon").setIndicator("Turuncu Salon").setContent(R.id.liner));
// Listview Group click listener
mytabhost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
if(tabId=="sosyaltesisler"){
prepareData("Sosyal Tesisler");
}else if(tabId=="yemekhane"){
prepareData("Yemekhane");
}else if(tabId=="turuncusalon"){
prepareData("Turuncu Salon");
}
}
});
getActivity().setTitle(Title);
return rootView;
}
private void prepareData(String Birim) {
// TODO Auto-generated method stub
int gunId = getArguments().getInt(SecimId);
List<String> liste = new ArrayList<String>();
for(int i = 0; i<yemek.length;i++){
if(yemek[i].getBirim().compareTo(Birim)==0 && yemek[i].getGun().compareTo(Gunler[gunId]) ==0 ){
liste.add(yemek[i].getAd()+"-"+yemek[i].getOgunAdi());
}
}
Menu = new String[liste.size()];
liste.toArray(Menu);
myAdapter = new MyCustomAdapter();
for(int i=0;i<Menu.length;i++){
if(myAdapter.checkSeparator(Menu[i].split("-")[1])){
myAdapter.addSeparatorItem(Menu[i].split("-")[1]);
}
myAdapter.addItem(Menu[i].split("-")[0]);
}
listView.setAdapter(myAdapter);
}
}`
我的CustomAdapter:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public boolean checkSeparator(String Separator){
for(int i=0;i<getCount();i++){
int type = getItemViewType(i);
if(type ==TYPE_SEPARATOR && getItem(i).compareTo(Separator)==0){
return false;
}
}
return true;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}`enter code here`
}
` 有人可以给我一个建议吗?