使用 Fragment Android 创建简单的 listView
Posted
技术标签:
【中文标题】使用 Fragment Android 创建简单的 listView【英文标题】:Create simple listView using Fragment Android 【发布时间】:2015-04-12 02:13:23 【问题描述】:我是 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_
android:layout_
android:background="#5ba4e5"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_
android:layout_ />
<ListView
android:id="@+id/listview"
android:layout_
android:layout_ />
</LinearLayout>
这是我的 single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:padding="5dp" >
<ImageView
android:id="@+id/icon"
android:layout_
android:layout_
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_
android:layout_
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;
【问题讨论】:
能不能也贴一下MobileArrayAdapter的代码 刚刚上传了mobileArrayAdapter.java 【参考方案1】:您的问题是如何检索ListView
,只需修改此行:
ListView listView = (ListView) getActivity().findViewById(R.id.listview);
看起来像这样:
ListView listView = (ListView) root.findViewById(R.id.listview);
列表视图是片段根视图的一部分,因此需要在该视图中找到它,而不是在您的 MainActivity 中
【讨论】:
但是TabsAdapter还有另一个问题。案例 1:返回新的 FavouriteFragment();它说该类型不能从 FavouriteFragment 转换为 Fragment FavouriteFragment 需要扩展 android.support.v4.app.Fragment以上是关于使用 Fragment Android 创建简单的 listView的主要内容,如果未能解决你的问题,请参考以下文章
android开发:多页面的实现 | Fragment的创建与使用