ViewPager inside every gridView cell - android

I'm trying to implement a sort of calendar with some timetables.
I created a GridView and since there may be multiple times for the same day I put in every cell a ViewPager. I created all the Adapters but there's a strange issue: the content of the pager is visible only in the first cell of the gridview.
If i put a simple TextView inside a cell so all the contents are shown properly in the 1st, 3rd and 5th cell.
Here are my files:
grid_row.xml : the layout of a single gridview cell:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/square"
android:orientation="vertical" >
<TextView
android:id="#+id/row_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-bold"
android:textColor="#cfd8dc"
android:textSize="9sp" />
<android.support.v4.view.ViewPager
android:id="#+id/row_orari_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
WhereFragment.java
[...]
public class GridViewCustomAdapter extends BaseAdapter {
ArrayList<Day> officeDay;
public GridViewCustomAdapter(Day[] days) {
officeDay = new ArrayList<Day>(Arrays.asList(days));
}
public int getCount() {
return 7;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView day = (TextView) row.findViewById(R.id.row_day);
ViewPager mPager = (ViewPager) row.findViewById(R.id.row_orari_pager);
day.setText(days[position]);
OrariPagerAdapter mPagerAdapter = new OrariPagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
return row;
}
public class OrariPagerAdapter extends FragmentStatePagerAdapter {
public ArrayList<Time> currentOrari;
public OrariPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public OrariObjectFragment getItem(int i) {
OrariObjectFragment fragment = new OrariObjectFragment(currentOrari);
Bundle args = new Bundle();
args.putInt(OrariObjectFragment.ARG_OBJECT, i);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
return "";
}
}
#SuppressLint("ValidFragment")
public class OrariObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
public ArrayList<Time> orari;
public OrariObjectFragment(ArrayList<Time> currentOrari) {
orari = currentOrari;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.orari_layout, container, false);
final int position = getArguments().getInt(ARG_OBJECT);
TextView orario = (TextView) rootView.findViewById(R.id.row_orario);
orario.setTextColor(Color.parseColor("#ff0000"));
return rootView;
}
}
}
orari_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/row_orario"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textColor="#607d8b"
android:textSize="15sp"
android:text="aoisjsdasdas"/>
</LinearLayout>
This is the result, so only the first cell is filled and not the other two.
Any advices? Thank you all

if (row == null) {
What about when row isnt null, ie, all the times after the first cell.
if (row == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
}
TextView day = (TextView) row.findViewById(R.id.row_day);
ViewPager mPager = (ViewPager) row.findViewById(R.id.row_orari_pager);
day.setText(days[position]);
OrariPagerAdapter mPagerAdapter = new OrariPagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);

if (row == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView day = (TextView) row.findViewById(R.id.row_day);
ViewPager mPager = (ViewPager) row.findViewById(R.id.row_orari_pager);
}
day.setText(days[position]);
OrariPagerAdapter mPagerAdapter = new OrariPagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);

Related

in gridview adapter class imageview set wrap content the images are blink while running in android?

I Created custom grid view with product list based on web service(json).but i facing new issue.when i set size(100dp) for image view height & width it working perfectly.but i set wrap_content or match parent it's view images are blink & blink again..
my fragment class :
public class HorizontalAdapter extends BaseAdapter {
private ArrayList<HomeCategoriesData> mArrayList;
private Context mContext;
private LayoutInflater mLayoutInflater;
public HorizontalAdapter(Activity homeActivity, ArrayList<HomeCategoriesData> mArrayList) {
this.mContext = homeActivity;
this.mArrayList = mArrayList;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mArrayList.size();
}
#Override
public Object getItem(int position) {
return mArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if (convertView == null) {
mViewHolder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.item_categories_item, null);
mViewHolder.mTvName = (TextView) convertView.findViewById(R.id.tv_categories_name);
mViewHolder.mIvimage = (ImageView) convertView.findViewById(R.id.iv_categories_img);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mTvName.setText(mArrayList.get(position).getName());
Picasso.with(mContext).load(mArrayList.get(position).getCat_img()).into(mViewHolder.mIvimage);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,mArrayList.get(position).getName(),Toast.LENGTH_SHORT).show();
//calling fragment from adatper class...
SellerSubCategoryFragment frag = new SellerSubCategoryFragment();
FragmentManager fm = ((MainActivity)mContext).getSupportFragmentManager();
Bundle bundle = new Bundle();
bundle.putString("id", String.valueOf(mArrayList.get(position).getId()));
frag.setArguments(bundle);
fm.beginTransaction().replace(R.id.content_frame,frag).addToBackStack(null).commit();
}
});
return convertView;
}
private class ViewHolder {
TextView mTvName;
ImageView mIvimage;
}
}
my adapter class xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_categories_img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/tv_categories_name"
style="#android:style/TextAppearance.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_top_categories_name_5"
android:gravity="center"
android:lines="1"
android:textColor="#color/black" />
</LinearLayout>
my out put issue are ,
so what i do? and what is the problem.why i cannot set wrap_content or match_parent in this grid view?. my custom grid view are ExpandapleHeightGridView

Custom GridView adapter changing image visibility status

I have a custom gridView and inside it i am having two images in a relative layout . Now when i click on the gridView i want the visibility of my images to change.
MainActivity:
public class Main extends Fragment implements OnMapReadyCallback{
GridView gridview,video_gridview;
GridViewAdapter adapter;
View v;
public Main() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_main,
container, false);
gridview = (GridView)v.findViewById(R.id.sub_notify_gridView);
gridview.setNumColumns(3);
adapter = new GridViewAdapter(getActivity(),FilePathStrings,
FileNameStrings,Image_Status,time);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int
position, long id) {
//here i need to know which image has been clicked
}
});
return v;
}
MainActivity Layout:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".35"
android:id="#+id/sub_notify_gridView">
<!--<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridViewImageid"/>-->
</GridView>
Cusotm GridView Adapter:
public class GridViewAdapter extends BaseAdapter {
// Declare variables
private Activity context;
private String[] filepath;
private String[] imageStatus;
private String Imgtime;
String[] separated;
private static LayoutInflater inflater = null;
public GridViewAdapter(FragmentActivity activity, String[] fpath, String[] fname, String[] image_status, String time) {
context = activity;
filepath = fpath;
imageStatus = image_status;
Imgtime = time;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
//TextView text = (TextView) vi.findViewById(R.id.text);
final ImageView image = (ImageView) vi.findViewById(R.id.image);
ImageView upload_image = (ImageView) vi.findViewById(R.id.upload_image);
TextView time = (TextView) vi.findViewById(R.id.textView);
time.setText(Imgtime);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setPadding(5, 3, 5, 2); //itrb
//text.setText(filename[position]);
Bitmap ThumbImage =ThumbnailUtils.extractThumbnail
(BitmapFactory.decodeFile(filepath[position]),
128, 128);
if(imageStatus[position].equals("0")){
upload_image.setVisibility(View.VISIBLE);
}
if (filepath[position] == null){
image.setImageResource(R.drawable.imageicon);
}
else{
if (position > 4){
image.setImageBitmap(ThumbImage);
}
else{
image.setImageBitmap(ThumbImage);
}
}
return vi;
}
GridView_item Layout File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:custom="http://schemas.android.com/tools"
android:padding="5dip">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignBottom="#+id/image"
android:layout_alignRight="#+id/image"
android:layout_alignEnd="#+id/image"
android:paddingRight="5dp"
android:paddingBottom="1.5dp"
android:id="#+id/textView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:paddingBottom="1.5dp"
android:id="#+id/upload_image"
android:visibility="gone"
android:src="#drawable/accept_icon"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Android View Pager and RecycleView

I just want to display a list using recycle view inside the view pager. (ex. just like android home page icons)
But here list is not showing and without view pager it works.
Please can anyone tell me where is the problem...
Thank you..
activity.xml
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
obj_tile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<ImageView
android:id="#+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#android:drawable/alert_light_frame" />
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="Group Title" />
</LinearLayout>
-fragment_screen_slide_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PageViewer Title" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
List<TilePojo> tilePojoList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
/*Context context=MainActivity.this;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_screen_slide_page, null);
RecyclerView rvTiles = (RecyclerView) findViewById(R.id.my_recycler_view);
for (int i = 0; i < 10; i++) {
TilePojo tilePojo = new TilePojo(R.drawable.apple, "Button Title " + i);
tilePojoList.add(tilePojo);
}
TileAdapter adapter = new TileAdapter(tilePojoList);
if(adapter!=null) {
rvTiles.setAdapter(adapter);
rvTiles.setLayoutManager(new GridLayoutManager(this, 2));
}
*/
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
-ScreenSlidePageFragment.java
public class ScreenSlidePageFragment extends Fragment {
List<TilePojo> tilePojoList=new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
View v = inflater.inflate(R.layout.fragment_screen_slide_page, null);
RecyclerView rvTiles = (RecyclerView) v.findViewById(R.id.my_recycler_view);
for (int i = 0; i < 10; i++) {
TilePojo tilePojo = new TilePojo(R.drawable.apple, "Button Title " + i);
tilePojoList.add(tilePojo);
}
TileAdapter adapter = new TileAdapter(tilePojoList);
if(adapter!=null) {
rvTiles.setAdapter(adapter);
rvTiles.setLayoutManager(new GridLayoutManager(this.getActivity(), 2));
}
return rootView;
}
}
TileAdapter.java
public class TileAdapter extends RecyclerView.Adapter<TileAdapter.ViewHolder> {
private List<TilePojo> list;
public TileAdapter(List<TilePojo> list) {
this.list = list;
}
#Override
public TileAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View tileView = inflater.inflate(R.layout.obj_tile, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(tileView);
return viewHolder;
}
#Override
public void onBindViewHolder(TileAdapter.ViewHolder holder, int position) {
TilePojo tilePojo=list.get(position);
holder.imageView.setImageResource(tilePojo.getSrc());
holder.nameTextView.setText(tilePojo.getTitle());
}
#Override
public int getItemCount() {
return list.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView nameTextView;
public ImageView imageView;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.tv_title);
imageView = (ImageView) itemView.findViewById(R.id.iv_icon);
}
}
}
I got the answer..
I just replace the following (#ScreenSlidePageFragment.java)
rootView.findViewById(R.id.my_recycler_view);
Thank you.

"Gridview Images" and "Full screen" fragments in multi pane(android)

I'm new android..my english little bit:(
I want develop multi pane app.
Two fragments "Gridview" and "Full screen"
I know multi pane, but i dont know Gridview and Full screen in multi pane.
Because all samples for ListView.
help me please
this app good for me but too complex=
http://www.codeproject.com/Articles/779293/Building-Dynamic-UI-for-Android-Devices
Its a simple implementation if i understand your question correctly
------------- Creating GridView ------------
For GridView you need an adapter for your GridView Object and images for columns, an example using country list and flag images,
Create drawable folder inside res and add country flag images inside drawable folder
Create gridlayout.xml in res/layout
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/border"
android:padding="5dp">
<ImageView
android:id="#+id/gridimage"
android:layout_height="65dp"
android:layout_width="65dp"
android:src="#drawable/icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:id="#+id/gridtext"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:ellipsize="marquee"></TextView>
</RelativeLayout>
Create gridrow.xml in res/Layout folder
<GridView
android:id="#+id/gridView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
Create GridAdapter.java class
public class GridAdapter extends BaseAdapter {
private ArrayList<String> CountryList;
private ArrayList<Integer> CountryFlag;
private Activity activity;
public GridAdapter(Activity activity,ArrayList<String> CountryList, ArrayList<Integer> CountryFlag){
super();this.CountryList = CountryList;
this.CountryFlag = CountryFlag;
this.activity = activity;}
#Override
public int getCount() {
return CountryList.size();
}
#Override
public String getItem(int position) {
return CountryList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.gridtext);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.gridimage);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(CountryList.get(position));
view.imgViewFlag.setImageResource(CountryFlag.get(position));
return convertView;
}
}
Create Fragment GridViewActivty
public class GridViewActivty extends Fragment {
private ArrayList CountryList = new ArrayList();
private ArrayList CountryFlag = new ArrayList();
private GridView mygrid;
private GridAdapter adapter;
private Context context;
public GridViewActivty(){}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{ View rootView = inflater.inflate(R.layout.gridlayout.xml, container, false);
CountryList = new ArrayList<String>();
CountryList.add("South Africa");
CountryList.add("Spain");
CountryList.add("Canada");
CountryList.add("China");
CountryList.add("France");
CountryList.add("Germany");
CountryList.add("Iran");
CountryList.add("Italy");
CountryList.add("Japan");
CountryList.add("Korea");
CountryList.add("Mexico");
CountryList.add("Netherlands");
CountryFlag = new ArrayList<Integer>();
CountryFlag.add(R.drawable.southafrica);
CountryFlag.add(R.drawable.spain);
CountryFlag.add(R.drawable.canada);
CountryFlag.add(R.drawable.china);
CountryFlag.add(R.drawable.france);
CountryFlag.add(R.drawable.germany);
CountryFlag.add(R.drawable.iran);
CountryFlag.add(R.drawable.italy);
CountryFlag.add(R.drawable.japan);
CountryFlag.add(R.drawable.korea);
CountryFlag.add(R.drawable.mexico);
CountryFlag.add(R.drawable.netherlands);
adapter = new GridAdapter(this.getActivity(),CountryList, CountryFlag);
mygrid = (GridView)rootView.findViewById(R.id.gridview1);
mygrid.setAdapter(adapter);
mygrid.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
}
);
return rootView;
}
}
----------- Creating Full Screen --------
Create fullscreen.xml inside Layout Folder
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
>
Create FullScreen.java Fragment
public class Fullscreen extends Fragment{ #Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{ View rootView = inflater.inflate(R.layout.fullscreen.xml, container, false);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
return convertView; }

Android GridView displaying incorrect item and fragment displaying on top of another fragment

I am implementing ActionBar Tab to display 3 tabs, each of which contains a GridLayout. The problem arises when the view is scrolled down, the hidden items appear but the order is somehow scrambled. The screen shows only 4 items initially (in a 2 column grid), but when the 5th comes into view, item 1 is shown instead, but the 6th is correct. The 7th and 8th items are also incorrect, and the error is similarly displayed on the other tabs. Scrolling across the tabs also jumbles up the order.
The second problem arises when I implement onItemClickListener on each item in the grid, which is supposed to replace the fragment with one showing larger view. Instead of replacing the view, it sits on top of the display with the bottom fragment showing through.
I can't figure out what's wrong. Here's my code
Main Activity:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
public static String[] mTabItems;
public static int[] mTabImages;
public static GridView mGridview;
public static String[][] itemName = new String[3][8];
public static int [][] itemPics = new int[3][8];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
int i = 0;
for (int tab = 1; tab<4; tab++){
for (int item = 1; item<9; item++){
String image = "tab" + tab + "_" + item;
int imageIden = getResources().getIdentifier(image, "drawable","com.tabgriddrawer");
itemPics[tab-1][item-1] = imageIden;
itemName[tab-1][item-1] = image;
i++;
}
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment frag = new SectionFragment();
Bundle arguments = new Bundle();
arguments.putInt(SectionFragment.ARG_TAB_NUMBER, i);
frag.setArguments(arguments);
return frag;
}
#Override
public int getCount() {
return 3;//set 3 tabs
}
#Override
public CharSequence getPageTitle(int position) {
return "Tab " + (position + 1);
}
}
public static class SectionFragment extends Fragment {
public static final String ARG_TAB_NUMBER = "tab_number";
public static Bundle detailArguments;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Bundle arg = getArguments();
int tab = arg.getInt(ARG_TAB_NUMBER);
mTabItems = new String[8];
mTabImages = new int[8];
for (int m=0; m<8; m++){
mTabItems[m] = itemName[tab][m];
mTabImages[m] = itemPics[tab][m];
}
View rootView = inflater.inflate(R.layout.fragment_section_gridview, container, false);
CustomGrid adapter = new CustomGrid(getActivity().getApplicationContext(), mTabItems,mTabImages);
mGridview = (GridView) rootView.findViewById(R.id.fragment_grid_view);
mGridview.setAdapter(adapter);
mGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id){
int t = arg.getInt(ARG_TAB_NUMBER);
Fragment itemDetailFragment = new ItemDetailFragment();
detailArguments = new Bundle();
detailArguments.putString("ItemName", itemName[t][position]);
detailArguments.putInt("ImageId", itemPics[t][position]);
itemDetailFragment.setArguments(detailArguments);
FragmentManager fm = getFragmentManager();
Fragment itemFragment = fm.findFragmentById(R.id.pager);
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.hide(itemFragment);
ft.replace(R.id.drawer_layout, itemDetailFragment, "detail");
ft.commit();
}
});
return rootView;
}
}
}
Custom Gridview adapter:
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] libraryItem;
private final int[] imageId;
public CustomGrid(Context c,String[] libItem,int[] Imageid ) {
mContext = c;
this.imageId = Imageid;
this.libraryItem = libItem;
}
#Override
public int getCount() {
return libraryItem.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fragment_gridview_item, null);
TextView textView = (TextView) grid.findViewById(R.id.txtItemName);
ImageView imageView = (ImageView)grid.findViewById(R.id.imageItem);
textView.setText(libraryItem[position]);
imageView.setImageResource(imageId[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
Item Detail Fragment:
public class ItemDetailFragment extends Fragment {
private Context mContext;
private String mItemDesc;
private int mImageId;
private String mItemName;
static int tab;
static int position;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_item_detail, container, false);
ImageView image = (ImageView)view.findViewById(R.id.itemimage);
image.setImageResource(mImageId);
TextView itemtext = (TextView)view.findViewById(R.id.itemName);
itemtext.setText(mItemName);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mImageId = (int)MainActivity.SectionFragment.detailArguments.getInt("ImageId");
mItemName = (String)MainActivity.SectionFragment.detailArguments.get("ItemName");
}
public void onBackPressed(){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main_frame, new MainActivity.SectionFragment());
ft.addToBackStack(null);
ft.commit();
}
}
Main XML
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- moved viewpager to inside drawer layout -->
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Gridview XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/fragment_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
I didn't look at your whole code. . . only the getView method in your Adapter, cause that is where I suspect the problem is.
When the convertView is not null, you are simply returning convertView. . . this is what is causing the problem. When you do this, you are returning a view that the adapter is attempting to recycle from one gridview position to another. That's why the image from one view is appearing in another. Instead, do this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fragment_gridview_item, null);
} else {
grid = (View) convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.txtItemName);
ImageView imageView = (ImageView)grid.findViewById(R.id.imageItem);
textView.setText(libraryItem[position]);
imageView.setImageResource(imageId[position]);
return grid;
}

Categories

Resources