'ViewPager not showing anything - android
I need some advice. ViewPager showing just white page. I have searched much, but I couldn't find solution for my case.
I tried to make simple button, but I got same output. I think it's not image problem.
Here's my code.
public class IntroduceArticleFragment extends Fragment {
View view;
String depart;
String path;
int num;
CustomPagerAdapter customPagerAdapter;
ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_introduce_article, container, false);
view.setBackgroundColor(Color.WHITE);
depart = getArguments().getString("depart");
create();
//customPagerAdapter = new CustomPagerAdapter(getActivity());
viewPager = (ViewPager)view.findViewById(R.id.pager);
viewPager.setAdapter(customPagerAdapter);
return view;
}
void create(){
DBHelper dbHelper = new DBHelper(getActivity());
path = dbHelper.getContentsPath(depart);
num = dbHelper.getNum(depart);
customPagerAdapter = new CustomPagerAdapter(getActivity());
customPagerAdapter.mResources = new int[num];
for(int i=0;i<num;i++) {
String uri = "@drawable/" + path + "_" + Integer.toString(i);
int imageResource = getActivity().getResources().getIdentifier(uri, "drawabale", getActivity().getPackageName());
customPagerAdapter.mResources[i] = imageResource;
}
}
}
public class CustomPagerAdapter extends PagerAdapter {
Context mContext;
public int[] mResources;
public CustomPagerAdapter(Context context) {
super();
mContext = context;
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = mLayoutInflater.inflate(R.layout.pager_item_0, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimageView_0);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
}
@Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
}
}
and here's xml code for ArticleFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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="0dp"
android:visibility="visible">
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
</LinearLayout>
and here's xml code for
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/pagerimageView_0" />
</LinearLayout>
Solution 1:[1]
Ok... First of all you must set the android:layout_height
of the ScrollView
's child to wrap_content
.
Second, Consider that you can't set android:layout_height
of ViewPager
to wrap_content
. Unless you use a custom ViewPager
. For Example you can check this answer
By setting android:fillViewport
to true in ScrollView
, The height of ScrollView
's Child automatically will set to match_parent
until its height become taller than screen.
And I don't get it why you set the height of ViewPager
to 0dp
and expect that it shows something!
Now your layout should be like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<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_pranet"
android:visibility="visible">
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
</LinearLayout>
Solution 2:[2]
I've spent half of day solving it and gets that my ViewPager have the same id with another ViewPager in another module in my app, so I've simply gave it another id.
Solution 3:[3]
You Have got it all wrong, use this code
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class CustomPagerAdapter extends FragmentStatePagerAdapter {
public int[] mResources;
public NewsAdapter(FragmentManager fm, int[] mResources) {
super(fm);
this.mNumOfTabs = mResources.length();
this.mResources = mResources;
}
@Override
public Fragment getItem(int position) {
return ContentFragment.newInstance(mResources[position]);
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
public class ContentFragment extends Fragment {
private Context context;
private int resource_id;
public static ContentFragment newInstance(int resourdid) {
Bundle args = new Bundle();
args.putInt("resource_id", resourdid);
ContentFragment fragment = new ContentFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
resource_id = getArguments().getInt("resource_id");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(in.newswallet.R.layout.content_row, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initalize();
}
private void initalize(){
View itemView = getView();
ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimageView_0);
imageView.setImageResource(resource_id);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context=context;
}
}
Solution 4:[4]
<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="0dp"
android:visibility="visible">
</android.support.v4.view.ViewPager>
try to change the android:layout_height = "0dp" to some value like android:layout_height = "50dp" or
give weight to the layout android:layout_weight = "1"
Solution 5:[5]
Change:
container.removeView((LinearLayout) object);
With:
container.removeView((ConstraintLayout) object);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | |
Solution 4 | Akhil Garg |
Solution 5 | Elletlar |