我認爲有以下可能是你所追求的(新行添加到getDropDownView
您設置的TextColor
的城市爲灰色後): -
@Override
public View getDropDownView (int position, View convertView, ViewGroup parent){
View view = super.getDropDownView(position, convertView, parent);
TextView spinnerTextView = (TextView) view;
if (position == 0) {
// Set the disable item text color
spinnerTextView.setTextColor(Color.BLACK);
} else {
spinnerTextView.setTextColor(Color.GRAY);
spinnerTextView.setOnclickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//YOUR ONCLICK HANDLING CODE HERE
}
});
}
return view;
}
PS在我上面的評論中,我提到了getView
作爲替代方案。這不是選擇視圖,而是下拉菜單(與ListViews
混淆)。
仔細查看您的答案和代碼。我看到你真的沒有任何地方。所以這裏有一個基於你的代碼的基本例子。
首先在下拉列表中輸入一個新的佈局文件(我也欺騙了一點,用於選定的條目,您可能每個都有單獨的佈局)。這是spinner_item.xml: -
<?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">
<TextView
android:id="@+id/spinnerTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
當你想定製功能你應該有一個自定義適配器(這是你出現在你的代碼中attemtping什麼)。適配器處理將數據(您的案例中的arrayList)放入微調器使用的視圖中(使用的術語是充氣)。所以這裏是我放入它自己的文件中的自定義適配器。這是MyArrayAdapter.java: -
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Mike092015 on 31/10/2016.
*/
public class MyArrayAdapter extends ArrayAdapter<String> {
private Context context;
private TextView district;
private ArrayList<String> districts;
public MyArrayAdapter(Activity context, int layout, ArrayList<String> districts) {
super(context, layout, districts);
this.context = context;
this.districts = districts;
}
@Override
public View getDropDownView(int position, View convertview, ViewGroup parent) {
View v = convertview;
if (v == null) {
v = LayoutInflater.from(this.context).inflate(R.layout.spinner_item,parent,false);
}
district = (TextView) v.findViewById(R.id.spinnerTextView);
district.setText(districts.get(position));
district.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"You clicked me",Toast.LENGTH_SHORT).show();
}
});
return v;
}
public View getView(int position, View convertview, ViewGroup parent) {
View v = convertview;
if (v == null) {
v = LayoutInflater.from(this.context).inflate(R.layout.spinner_item,parent,false);
}
district = (TextView) v.findViewById(R.id.spinnerTextView);
district.setText(districts.get(position));
return v;
}
}
注意在getDropDownView
的onClickListener
這是你捕捉和處理條目的點擊(在這種情況下,它只是發出一個敬酒)。
activity_main.xml中是(包括微調,小,如果從你的佈局有什麼不同的佈局): -
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing"/>
<Spinner
android:id="@+id/districtSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="District"
android:textSize="16sp"
/>
</LinearLayout>
最後的活動,來測試這個我用MainActivity,所以MainActicity.java是: -
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Spinner;
import java.util.ArrayList; import java.util。陣列;
/** *由Mike092015於2016年3月6日創建。 */ 公共類MainActivity擴展AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner DistrictBetterSpinner;
String[] District_Spinner = new String[]{"Punjab", "Rawalpindi", "Attock", "Faisalabad", "Chakwal", "Bhakkar", "Jhelum", "Multan", "Chiniot"
, "Bhawalpur", "Jhelum", "Gujrat", "Gujranwala", "Lahore", "Mianwali", "Khyber Pakhtunkhwa (KPK)", "Abbotabad", "Haripur", "Kohat",
"Sindh", "Hyderabad", "Jacobabad", "Balochistan", "Awaran", "Gawadar", "Federally Administered Tribal Areas", "Khyber Agency",
"Mohmat Agency", "Azad Jammu and Kashmir", "Neelum", "Bagh", "Gilgit Baltistan", "Skardu", "Astore"};
ArrayList<String> spinnerlist;
MyArrayAdapter arrayadapter;
DistrictBetterSpinner = (Spinner) findViewById(R.id.districtSpinner);
spinnerlist = new ArrayList<>(Arrays.asList(District_Spinner));
arrayadapter = new MyArrayAdapter(this,R.layout.spinner_item,spinnerlist);
arrayadapter.setDropDownViewResource(R.layout.spinner_item);
DistrictBetterSpinner.setAdapter(arrayadapter);
}
}
注也有一些變化,但它是非常根據你的代碼。
不太確定你的問題是什麼。請添加額外的細節並澄清問題。謝謝! –
我想讓一些項目可點擊並且有些不可點擊。例如國家名稱應該不可點擊,但城市名稱應該可點擊 – Sara
我已編輯並提出問題。請看一看。 – Sara