简体   繁体   中英

Fragment can't show image

I am very new to using Fragments in Android, and trying to build a simple image slider for my apps, using View Pager. I already have the code and it's not working, the image is not being shown when apps run, anyone can help me solve this. This is my code

fragment_home.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"
tools:context="com.example.wk.sigah.FragmentHome"
android:background="@color/colorPrimary">

<!-- TODO: Update blank fragment layout -->
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:id="@+id/viewImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
</ScrollView>

FragmentHome.java

public class FragmentHome extends Fragment {
ViewPager viewPager;
customSwitchAdapterHome adapter;
public FragmentHome() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    /**return inflater.inflate(R.layout.fragment_fragment_home, container, false);*/
    View view=inflater.inflate(R.layout.fragment_fragment_home,container,false);
    viewPager=(ViewPager)view.findViewById(R.id.viewImage);
    adapter=new customSwitchAdapterHome(this.getActivity());
    viewPager.setAdapter(adapter);
    return view;
}

customSwitchAdapterHome.java

public class customSwitchAdapterHome extends PagerAdapter {
private int[] image_resource={R.drawable.background_bottom_nav,R.drawable.background_login_two};
private Context ctx;
private LayoutInflater layoutInflater;

public customSwitchAdapterHome(Context ctx){
    this.ctx=ctx;
}

@Override
public int getCount() {
    return image_resource.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return(view==(LinearLayout)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view=layoutInflater.inflate(R.layout.swipe_layout,container,false);
    ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
    TextView textView=(TextView)item_view.findViewById(R.id.image_count);
    imageView.setImageResource(image_resource[position]);
    textView.setText(" "+position);
    container.addView(item_view);
    return item_view;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
   container.removeView((LinearLayout)object);
}

swipe_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/image_count"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:text="Hello World" />
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"/>
</LinearLayout>

This is because the TextView in the LinearLayout with Vertical orientation is of height match_parent the imageView cannot fit in. So, to solve you can use wrap_content to textView or ,use

  android:layout_weight="1"

and use

  android:layout_height="0dp"

to both textView and imageView in the linear layout

Your layout is messed up. Try the layout below . Set TextView height as wrap_content . If you set height as match_parent then it will cover whole LinearLayout so ImageView will not be displayed .

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">
            <TextView
                android:id="@+id/image_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginBottom="20dp"
                android:text="Hello World" />
            <ImageView
                android:id="@+id/image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </LinearLayout>

This is just an example cause i have no idea what exactly the ui will be .Also Layouts are basic building block So you should start reading User Interface & Navigation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM