使用Fragment Android创建简单的listView

时间:2015-02-11 16:03:54

标签: android eclipse listview

我是listView的新手并搜索了很多教程,但很困惑,因为教程教的是不同的风格,而且很复杂。基本上我想按下收藏夹选项卡时用图像和文本创建一个listView。

我唯一困惑的是FavouriteFragment.java的编码,因为我不确定它是否正确。 listView没有出现。希望有人可以帮助我。

这是我的activity_favourite_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5ba4e5"
android:orientation="vertical" >

<TextView
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

这是我的single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="90px"
    android:layout_height="60dp"
    android:layout_marginLeft="5px"
    android:layout_marginRight="30px"
    android:layout_marginTop="5px"
    android:layout_weight="0.68"
    android:src="@drawable/ic_launcher" >

</ImageView>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:layout_gravity="center"
    android:text="@+id/label"
    android:textSize="30px" />

</LinearLayout>    

MainActivity.java

import android.app.ActionBar;
import android.content.Intent;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import com.example.sgrecipe.TabsAdapter;

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Recipes", "Favourites", "Quiz" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

// insert option menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

FavouriteFragment.java

import com.example.sgrecipe.MobileArrayAdapter;
import android.app.Fragment;
import android.app.ListActivity;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FavouriteFragment extends Fragment{

String[] Recipe = new String[] { "Chinese Food", "Malay Food", "Indian        Food", "Others"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle    savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_favourite_fragment, null);

    MobileArrayAdapter adapter = new MobileArrayAdapter(this.getActivity(), Recipe);
    ListView listView = (ListView) getActivity().findViewById(R.id.listview);

    listView.setAdapter(adapter);

    return root;
}
}

MobileArrayAdapter.java

import com.example.sgrecipe.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.single_row, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.single_row,
            parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change icon based on name
    String s = values[position];
    System.out.println(s);
    if (s.equals("Chinese Food")) {
        imageView.setImageResource(R.drawable.chinese);
    } else if (s.equals("Malay Food")) {
        imageView.setImageResource(R.drawable.malay);
    } else if (s.equals("Indian Food")) {
        imageView.setImageResource(R.drawable.indian);
    } else {
        imageView.setImageResource(R.drawable.others);
    }
    return rowView;
}
}

1 个答案:

答案 0 :(得分:3)

您的问题是如何检索ListView,只需修改此行:

ListView listView = (ListView) getActivity().findViewById(R.id.listview);

看起来像这样:

ListView listView = (ListView) root.findViewById(R.id.listview);

列表视图是片段根视图的一部分,因此需要在该视图中找到它,而不是在MainActivity中找到