视图寻呼机中的交错网格视图未正确设置适配器

时间:2015-06-01 08:03:07

标签: android android-fragments android-viewpager staggered-gridview

我的交错网格视图适配器在api 19或更高版本中正常工作。但是当我在api 17或更低版​​本中运行时,我看到适配器未正确设置。如果我用Staggered Grid View更改Grid View它工作正常,它表明我的adpter类工作正常。但我不知道我的问题在哪里。图像没有在交错网格视图中设置。可能是因为我的视图寻呼机 tis是我的标签视图XML with viewpager:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ir.hisis.cloth.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/ColorPrimary"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

</LinearLayout>

这是我的Staggered Grid View Xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.etsy.android.grid.StaggeredGridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:item_margin="8dp"
        app:column_count="1" />
</FrameLayout>

这是我的主要活动:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("");

        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new TabsPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

标签页适配器:

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

    CharSequence Titles[];
    int NumbOfTabs;

    public TabsPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
        super(fm);

        this.Titles = mTitles;
        this.NumbOfTabs = mNumbOfTabsumb;

    }
    @Override
    public Fragment getItem(int position) {

        if(position == 0)
        {
            return new MainFragment();
        }
        else if(position == 1)
        {
            return new MainFragment();
        }
        else
        {
            return new MainFragment();
        }

    }


    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }


    @Override
    public int getCount() {
        return NumbOfTabs;
    }
}

和主要片段:

    public  class MainFragment extends Fragment {

        public MainFragment() {

        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }


        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            ImageAdapter adapt;
            StaggeredGridView gridView = (StaggeredGridView) getView().findViewById(R.id.grid_view);
            adapt= new ImageAdapter(getActivity());
            gridView.setAdapter(adapt);
        }
image adpter:
public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public String TAG = "base adapter";
    Activity activity;
    public ImageAdapter(Activity a) {
        activity = a;
        mContext = activity.getApplicationContext();
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View imageItem;
        Log.e("item","");
        if (convertView == null) {
            LayoutInflater inflater = activity.getLayoutInflater();
            imageItem = inflater.inflate(R.layout.image_item, null);

            ImageItemViewHolder tag = new ImageItemViewHolder();
            tag.image = (ImageView) imageItem.findViewById(R.id.image);
            tag.tagsContainer = (LinearLayout) imageItem.findViewById(R.id.tags_container);
            tag.likeImage = (ImageView) imageItem.findViewById(R.id.like_image);
            setShareLongClickListenerForImage(tag.image);
            imageItem.setTag(tag);
        } else {
            imageItem =  convertView;
        }

        ImageItemViewHolder tag = (ImageItemViewHolder) imageItem.getTag();
        ImageView img = tag.image;
        img.setImageResource(mThumbIds[position]);

        return imageItem;
    }

    private void setShareLongClickListenerForImage(ImageView image){
        image.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View arg0) {
                try{
                    Log.wtf(TAG, "starting to share image");
                    Utils.shareImage((ImageView) arg0, activity);
                }
                catch(FileNotFoundException e){
                    Toast.makeText(arg0.getContext(), activity.getString(R.string.external_storage_is_uavailable), Toast.LENGTH_LONG).show();
                }catch (IOException e) {
                    Toast.makeText(arg0.getContext(), activity.getString(R.string.proble_with_external_storage), Toast.LENGTH_LONG).show();
                }

                return true;
            }
        });
    }


    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };

    public static class ImageItemViewHolder{
        public ImageView image;
        public LinearLayout tagsContainer;
        public ImageView likeImage;
    }

2 个答案:

答案 0 :(得分:2)

将minSdkVersion的值更改为库项目清单到9

<uses-sdk android:minSdkVersion="10"/>

<uses-sdk android:minSdkVersion="9"/>

答案 1 :(得分:1)

您可以克隆git项目并将其作为本地库项目添加到您的应用程序中。由于这将是本地图书馆项目,你可以看到有一个清单,你可以在 github.com/etsy/AndroidStaggeredGrid/blob/master/library/src / ...看到。我没有&#39;知道我们是否可以从依赖标签修改minsdk值。寻找,如果能找到,将更新你。