Code Bye

Fragment上添加Listview的问题

      题主是个非计算机专业的苦逼大四狗,毕业设计给本人挖了个坑,开发个安卓仓库管理系统
想在Fragment中添加ListView,程序运行后,Fragment显示不出本人想要的Listview,不知道问题出在哪里,求大神指点
这是本人的Fragment代码
package echo.com.wms;
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.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by echo on 2017/4/17.
 */
public class InventoryManagementFragment extends Fragment{
    private String[] values = new String[] { "入库管理", "出库管理" };
    private int[] images = new int[] { R.drawable.img_putin, R.drawable.img_putout };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_inventorymanagement, container, false);
        ListView list = (ListView)view.findViewById(R.id.listview);
        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < values.length; i++) {
            Map<String, Object> listItem = new HashMap<String, Object>();
            listItem.put("values", values[i]);
            listItem.put("images", images[i]);
            listItems.add(listItem);
        }
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), listItems,
                R.layout.fragment_inventorymanagement, new String[] { "values", "images" },
                new int[] { R.id.text_item, R.id.image_item });
        list.setAdapter(adapter);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}

这是本人的Activity代码

package echo.com.wms;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
public class MenuActivity extends AppCompatActivity {
    private InventoryManagementFragment inventoryManagementFragment;
    private WarehouseInformationFragment warehouseInformationFragment;
    private UserInformationFragment userInformationFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottomNavigationBar
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
                );
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_home_white_24dp, "库存管理").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.mipmap.ic_find_replace_white_24dp, "仓库信息").setActiveColorResource(R.color.green))
                .addItem(new BottomNavigationItem(R.mipmap.ic_tv_white_24dp, "用户设置").setActiveColorResource(R.color.blue))
                .setFirstSelectedPosition(0)
                .initialise();
        //设置默认页面
        setDefaultFragment();
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {//未选中 -> 选中
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                hideFragments(fragmentTransaction);
                switch (position) {
                    case 0:
// 假如fg1为空,则创建一个并添加到界面上
                        if (inventoryManagementFragment == null) {
                            inventoryManagementFragment = new InventoryManagementFragment();
                            fragmentTransaction.add(R.id.layFrame,inventoryManagementFragment);
                        } else {
// 假如不为空,则直接将它显示出来
                            fragmentTransaction.show(inventoryManagementFragment);
                        }
                        break;
                    case 1:
                        if (warehouseInformationFragment == null) {
                            warehouseInformationFragment = new WarehouseInformationFragment();
                            fragmentTransaction.add(R.id.layFrame,warehouseInformationFragment);
                        } else {
                            fragmentTransaction.show(warehouseInformationFragment);
                        }
                        break;
                    case 2:
                        if (userInformationFragment == null) {
                            userInformationFragment = new UserInformationFragment();
                            fragmentTransaction.add(R.id.layFrame,userInformationFragment);
                        } else {
                            fragmentTransaction.show(userInformationFragment);
                        }
                        break;
                }
                fragmentTransaction.commit(); // 提交
            }
            @Override
            public void onTabUnselected(int position) {//选中 -> 未选中
            }
            @Override
            public void onTabReselected(int position) {//选中 -> 选中
            }
        });
    }
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (inventoryManagementFragment != null) {
            fragmentTransaction.hide(inventoryManagementFragment);
        }
        if (warehouseInformationFragment != null) {
            fragmentTransaction.hide(warehouseInformationFragment);
        }
        if (userInformationFragment != null) {
            fragmentTransaction.hide(userInformationFragment);
        }
    }
    //设置默认的页面
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        inventoryManagementFragment = new InventoryManagementFragment();
        transaction.add(R.id.layFrame,  inventoryManagementFragment);
        transaction.commit();
    }
}

运行后的情况

解决方案

40

引用:

所以你引用错了布局,要使用这个item的布局放进adapter中。(就是包含R.id.text_item,R.id.image_item这两个组件的layout)


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Fragment上添加Listview的问题