Imageview无法使用新图像进行更新

时间:2017-09-24 11:25:55

标签: android

我的布局有八个不同的imageView s,并由八个不同的图像(基于图层)加载。在布局的底部,我使用gridview通过适配器加载另外五个图像。这里有截图.. application layout。当用户点击gridview图片时,相应的图片应加载到imageview。但现在,当用户点击每个gridview图片时,一切都没有发生。不知道我哪里出错了。

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.keikomat.austurn.keikomaterialdesign.TwoFragment">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="71dp"
        app:srcCompat="@drawable/face1" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        app:srcCompat="@drawable/eyes1" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/eb1"
        android:layout_alignTop="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ns1"
        android:layout_alignTop="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        app:srcCompat="@drawable/ear1" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        app:srcCompat="@drawable/lips1" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        app:srcCompat="@drawable/mst1" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignStart="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        app:srcCompat="@drawable/hair1" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageView1" />

    <GridView
        android:id="@+id/gridviewShapes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/spinner"
        android:columnWidth="90dp"
        android:numColumns="5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

这是我的Java:

public class TwoFragment extends Fragment {
    private ImageView head;

    ArrayList<Integer> item_ids = new ArrayList<Integer>();

    public TwoFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view1 = inflater.inflate(R.layout.fragment_two,container,true);
        View view = inflater.inflate(R.layout.fragment_two,container,false);
        GridView gridView = (GridView) view.findViewById(R.id.gridviewShapes);
        gridView.setAdapter(new GridViewAdapterFace(view.getContext())); // uses the view to get the context instead of getActivity().
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v,int position, long id) {
                if (position == 0) {
                    // RelativeLayout im =(RelativeLayout)view1;
                    ImageView i=(ImageView)view1.findViewById(R.id.imageView1);
                     i.setImageResource(R.drawable.face5);
                } else if (position == 1) {

                } else if (position == 2) {

                } else if (position == 3) {

                } else if (position == 4) {

                } else if (position == 5) {

                }
            }
        });

        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

将此添加到所有图像视图

android:focusable="false"
android:focusableInTouchMode="false"

尝试像这样设置图像

i.setImageDrawable(getResources().getDrawable(R.drawable.star_off));