我的交错网格视图适配器在 api 19 或更高版本中正常工作。但是当我在 api 17 或更低版本中运行时,我发现适配器设置不正确。如果我更改它工作正常,它表明我的适配器Staggered Grid View
类 Grid View
工作正常。但我不知道我的问题在哪里。未在交错网格视图中设置的图像。可能是因为我的视图寻呼机 tis 是我的带有 viewpager 的选项卡视图 XML:
<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>
这是我的交错网格视图 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;
}