网上的方法基本上都试过了,没效果。
适配器能进getView(),返回的count也正常、
```public class Index extends AppCompatActivity {
boolean flag = true;//判断数据是否第一加载
private List<listView> listView = new ArrayList<listView>();
private ListFragment listFragment = new ListFragment();
private MineFragment mineFragment = new MineFragment();
private OrderFragment orderFragment = new OrderFragment();
Fragment currentFragment = listFragment;
FragmentManager fm = getSupportFragmentManager();
Fragment f = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
//解析菜单文件
initData();
final ListViewAdapter listViewAdapter = new ListViewAdapter(Index.this,R.layout.listlayout,listView);
final ListView dataList = findViewById(R.id.Data_list);
dataList.setAdapter(listViewAdapter);
//导航图片设置初始化
ImageView imageView1 = findViewById(R.id.nav_index);
ImageView imageView2 = findViewById(R.id.nav_order);
ImageView imageView3 = findViewById(R.id.nav_mine);
View.OnClickListener listener= new OnClickListener(){
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.nav_index:
listViewAdapter.notifyDataSetChanged();
dataList.setAdapter(listViewAdapter);
showFragment(listFragment);
break;
case R.id.nav_mine:
showFragment(mineFragment);
break;
default:
showFragment(orderFragment);
break;
}
}
};
imageView1.setOnClickListener(listener);
imageView2.setOnClickListener(listener);
imageView3.setOnClickListener(listener);
}
private void showFragment(Fragment fragment){
FragmentTransaction ft = fm.beginTransaction();
if (currentFragment != fragment){// 判断传入的fragment是不是当前的currentFragmentgit
ft.hide(currentFragment);// 不是则隐藏
currentFragment = fragment; // 然后将传入的fragment赋值给currentFragment
if (!fragment.isAdded()){ // 判断传入的fragment是否已经被add()过
ft.add(R.id.fragment,fragment).show(fragment).commit();
}else{
ft.show(fragment).commit();
}
}
}
private void initData() {
listView list = new listView("系统提示","测试内容信息","2019-3-14",R.drawable.shop1);
listView.add(list);
listView list1 = new listView("二号餐厅","测试内容信息","2019-3-14",R.drawable.shop2);
listView.add(list1);
}
}
适配器代码
public class ListViewAdapter extends ArrayAdapter {
private final int resourceId;
public ListViewAdapter(Context context, int resource, List<listView> objects ) {
super(context, resource, objects);
this.resourceId = resource;
}
@Override
public View getView(int position,View convertView, ViewGroup parent) {
listView listView = (listView) getItem(position);// 获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId,null);//实例化一个对象
ImageView listImage = (ImageView) view.findViewById(R.id.shop_img);//获取该布局内的图片视图
TextView listText = (TextView) view.findViewById(R.id.shop_name);//获取该布局内的文字视图
listImage.setImageResource(listView.getImageId());//设置资源
listText.setText(listView.getTitle());
System.out.println("被调用");
return view;
}
}
listview的Xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/index_gray">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@color/index_white"
android:id="@+id/Data_list">
</ListView>
</LinearLayout>
</FrameLayout>
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/con_index"
android:layout_width="match_parent"
android:background="@color/index_gray"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="600dp"
android:name="com.example.administrator.app.ListFragment"
android:id="@+id/fragment"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="60dp"
android:padding="5dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="@color/index_white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/nav_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/shouye"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/nav_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/dingdan"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/nav_mine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/wode"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>