在片段内单击按钮时动态修改图像视图

时间:2015-06-01 03:05:26

标签: android android-fragments

package com.example.layout.layout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;

import org.w3c.dom.Text;


/**
 * A placeholder fragment containing a simple view.
 */
public class MovieInfoFile extends Fragment {
    public static final String ARG_OPTION="argument_option";
    HashMap[]items=new HashMap[30];
    int i;
    public ImageView image;
    TextView name;
    TextView des;
    TextView stars;
    TextView year;
    TextView rating;
    public static MovieInfoFile newInstance(int option)
    {
        MovieInfoFile fragment=new MovieInfoFile();
        Bundle args=new Bundle();

        args.putInt(ARG_OPTION,option);
        fragment.setArguments(args);



        return fragment;

    }

    public MovieInfoFile() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.movieinfolayout, container, false);
        Button loadnext=(Button) view.findViewById(R.id.next);
         image=(ImageView) view.findViewById((R.id.image1));
         name=(TextView) view.findViewById(R.id.nameans);
         des=(TextView) view.findViewById(R.id.desans);
         stars=(TextView) view.findViewById(R.id.starans);
         year=(TextView) view.findViewById(R.id.yearans);
         rating=(TextView) view.findViewById(R.id.ratingans);
         enter code hereView.OnClickListener aButtonChangeImageListener=new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getId()==R.id.next)
                {

                    int id=(Integer)items[1].get("image");
                    image.setImageResource(id);
                }



            }
        };





        return view;
    }
}



Here is the layout (XML file)

<?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"
    android:layout_gravity="center"
    android:background="@drawable/baground"
    android:weightSum="1">
    <ImageView
        android:layout_width="315dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
      android:src="@drawable/avatar"
        android:id="@+id/image1"
        android:layout_weight="0.53" />
     <TableLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginTop="50dp"
         android:layout_gravity="center_horizontal"
         android:id="@+id/table1">
         <TableRow>
             <TextView
                 android:text="Movie Name: "
                 android:textStyle="bold"
                 android:id="@+id/name"

                 />
             <TextView
                 android:text="Movie Name: "
                 android:textStyle="bold"
                 android:id="@+id/nameans"

                 />
             </TableRow>
         <TableRow>
             <TextView
                 android:text="Description: "
                 android:textStyle="bold"
                 android:id="@+id/des"

                 />
             <TextView
                 android:text="Description: "
                 android:textStyle="bold"
                 android:id="@+id/desans"

                 />
         </TableRow>
         <TableRow>
             <TextView
                 android:text="Stars "
                 android:textStyle="bold"
                 android:id="@+id/star"

                 />
             <TextView
                 android:text="Stars "
                 android:textStyle="bold"
                 android:id="@+id/starans"

                 />
         </TableRow>
         <TableRow>
             <TextView
                 android:text="Year"
                 android:textStyle="bold"
                 android:id="@+id/year"


                 />
             <TextView
                 android:text="Year"
                 android:textStyle="bold"
                 android:id="@+id/yearans"

                 />
         </TableRow>

         <TableRow>
             <TextView
                 android:text="Rating"
                 android:textStyle="bold"
                 android:id="@+id/rating"

                 />
             <TextView
                 android:text="Rating"
                 android:textStyle="bold"
                 android:id="@+id/ratingans"

                 />
         </TableRow>


     </TableLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LoadPrev"
        android:id="@+id/prev"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loadnext"
        android:layout_gravity="center_horizontal"
        android:id="@+id/next"/>
</LinearLayout>

我正在尝试在片段内单击按钮时动态更改图像视图。但是,当我这样做时,它给了我错误,“不幸的是你的应用程序停止了”。任何人都可以帮助我,并告诉我在片段中点击按钮动态修改视图的正确方法。

1 个答案:

答案 0 :(得分:1)

对于按钮加载(我猜点击此图片后应该更改),所以将 aButtonChangeImageListener 传递给Button loadNext,如下所示:

loadNext.setOnClickListener(aButtonChangeImageListener);
返回视图行之前

相关问题