修复网格视图中的列数和行数

时间:2016-02-24 05:52:45

标签: java android android-fragments android-gridview

enter image description here

我列出了Fragment中设备上安装的所有应用程序,里面有gridview。我想要做的是,我想要4行* 5行(仅限一个片段中的20个App),如果我安装新的应用程序(应用计数21),它必须设置在下一个片段并设置在新片段的左上角,片段应该动态生成,并添加到viewpager。

这是我的代码。 MainActivity:

public class MainActivity extends FragmentActivity {

    ViewPager pager;
    AppViewAdapter adapter;
    GridFrag gridFrag;
    FragmentManager fragmentManager;
    FragmentTransaction transaction;

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

    private void bindView() {
        pager = (ViewPager) findViewById(R.id.slideingpager);
    }

    private void init() {
        adapter = new AppViewAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
    }


}

GridFragment.java:

public class GridFrag extends android.support.v4.app.Fragment implements
        OnItemClickListener {

    GridView gridView;
    ViewAdapter viewAdapter;
    PackageManager packageManager;
    int columnsOfGridView;

    public class AppDetails {
        Drawable icon;
        String label;
    }

    AppDetails packages[];

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.gridfrag, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        packageManager = getActivity().getPackageManager();

        gridView = (GridView) getActivity().findViewById(R.id.grid);

        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> packList = packageManager.queryIntentActivities(
                mainIntent, 0);
        packages = new AppDetails[packList.size()];
        for (int i = 0; i < packList.size(); i++) {
            packages[i] = new AppDetails();
            packages[i].icon = packList.get(i).loadIcon(packageManager);
            packages[i].label = packList.get(i).loadLabel(packageManager)
                    .toString();

        }
        viewAdapter = new ViewAdapter(getActivity(), packages);
        gridView.setAdapter(viewAdapter);
        int totalApps = gridView.getCount();
        Toast.makeText(getActivity(), "Total "+ totalApps + " App(s)Found",Toast.LENGTH_SHORT).show();
        gridView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(),
                "You Clicked on : " + packages[position].label,
                Toast.LENGTH_SHORT).show();
    }

}

ViewAdapter.java(GridAdapter)

public class ViewAdapter extends BaseAdapter {

    private Context context;
    AppDetails setPacksForAdapter[];

    public ViewAdapter(Context c, AppDetails apps[]) {
        context = c;
        setPacksForAdapter = apps;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return setPacksForAdapter.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {
        ImageView imgDefaultAppIcon;
        TextView tvDefaultAppLabel;
    }

    @Override
    public View getView(int pos, View v, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = new ViewHolder();
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.grid_view, null);
            holder.imgDefaultAppIcon = (ImageView) v
                    .findViewById(R.id.ivAppIcon);
            holder.tvDefaultAppLabel = (TextView) v
                    .findViewById(R.id.tvAppLabel);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.imgDefaultAppIcon.setImageDrawable(setPacksForAdapter[pos].icon);
        holder.tvDefaultAppLabel.setText(setPacksForAdapter[pos].label);

        return v;
    }

}

AppViewAdepter.java(适用于ViewPager)

public class AppViewAdapter extends FragmentPagerAdapter {

    List<Fragment> fragmentsList;

    public AppViewAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentsList = new ArrayList<Fragment>();
        for (int i = 0; i <= 2; i++) {
            if (i % 2 == 0)
                fragmentsList.add(new GridFrag());
            else
                fragmentsList.add(new FragTwo());

        }
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragmentsList.get(arg0);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return fragmentsList.size();
    }
}

2 个答案:

答案 0 :(得分:0)

  

getView()方法

中的适配器中设置Layoutparams动态单个网格根视图的高度
(Device height / 5) - statusBarHieght
适配器的getView()中的

部分代码

            int grid_height = ((Helper.get_device_height(context)) / 5) - statusBarHeight;
            GridView.LayoutParams layoutParams = (android.widget.AbsListView.LayoutParams) view.getLayoutParams();
            layoutParams.height = grid_height;
            view.setLayoutParams(layoutParams);

  

gridview XML set columns=4

它将解决您的问题。

希望它会对你有所帮助!

答案 1 :(得分:0)

默认情况下layout.xml中设置了

<android:columnCount="auto_fit">,您必须将其更改为4 {更改为<android:columnCount="4">并在java中设置相似的行,您可以执行此操作,如下所示setColumnCount(4)setRowCount(5)