Android Spinner:点击显示或隐藏imageview

时间:2016-12-12 19:18:49

标签: android android-spinner android-custom-view

我有一个文本视图和一个图像视图的微调器,当点击其中一个项目时我只想显示文本而不是图像视图,我如何获得图片视图以及我有自定义微调器布局,任何帮助将不胜感激:

spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single Item Design -->
<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/txt"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:padding="10dp"
        android:layout_gravity="center_vertical"
        >
    </TextView>
    <ImageView
        android:id="@+id/img"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="10dp"
        android:layout_gravity="center_vertical"
        />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="10dp" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    int count=0;
    int pre_pos=-1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<ItemData> list = new ArrayList<>();
        list.add(new ItemData("EUR", R.drawable.ger));
        list.add(new ItemData("USD", R.drawable.usa));
        list.add(new ItemData("JPY", R.drawable.jap));

        list.add(new ItemData("GBP", R.drawable.gb));
        list.add(new ItemData("AUD", R.drawable.aus));
        list.add(new ItemData("CHF", R.drawable.swiss));
        list.add(new ItemData("CAD", R.drawable.can));
        list.add(new ItemData("SEK", R.drawable.sweden));
        list.add(new ItemData("NZD", R.drawable.newz));
        list.add(new ItemData("KRW", R.drawable.skorea));

        //list.add(new ItemData("Usd",R.drawable.usd));
        //list.add(new ItemData("Jpy",R.drawable.jpy));
        //list.add(new ItemData("Aud",R.drawable.aud));
        Spinner sp = (Spinner) findViewById(R.id.spinner);
        SpinnerAdapter adapter = new SpinnerAdapter(this,
                R.layout.spinner_layout, R.id.txt, list);

        ArrayList<ItemData> list2= new ArrayList<>();
        list2.add(new ItemData("EUR", R.drawable.imag1));
        list2.add(new ItemData("USD", R.drawable.imag2));
        list2.add(new ItemData("JPY", R.drawable.imag3));


//listener of spinner
 sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // how could i hide the image view of selected item

            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }


}


}

SpinnerAdapter.java

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by Abbas on 12/11/2016.
 */
public class SpinnerAdapter extends ArrayAdapter<ItemData> {
    static int count=0;
    int groupid;
    Activity context;
    ArrayList<ItemData> list;
    LayoutInflater inflater;
    SharedPreferences sharedPreferences;

    public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemData>
            list){
        super(context,id,list);
        this.list=list;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.groupid=groupid;
    }

    public View getView(int position, View convertView, ViewGroup parent ){
        View itemView=inflater.inflate(groupid,parent,false);


        ImageView imageView=(ImageView)itemView.findViewById(R.id.img);
        imageView.setImageResource(list.get(position).getImageId());
        TextView textView=(TextView)itemView.findViewById(R.id.txt);
        textView.setText(list.get(position).getText());
        return itemView;
    }

    public View getDropDownView(int position, View convertView, ViewGroup
            parent){


        return getView(position,convertView,parent);

    }
}

0 个答案:

没有答案