gpt4 book ai didi

android - 如何从android中的微调器列表中隐藏所选项目

转载 作者:行者123 更新时间:2023-11-29 02:42:08 26 4
gpt4 key购买 nike

我想从微调器列表中隐藏所选项目只是这个问题 Hide the selected item from the custom spinner list但我没有得到答案。我只想完全按照我的代码链接中的问题做......

toolbar.xml

<android.support.v7.widget.Toolbar 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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>

<Spinner
android:id="@+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>

toolbarActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c9c9c9"
android:orientation="vertical" >

<include
android:id="@+id/toolbar"
layout="@layout/spintoolbar" />


</LinearLayout>

spinnerAdapter

public class CustomSpinnerAdapter extends ArrayAdapter<String> {

private Context context1;
private ArrayList<String> data;
public Resources res;
LayoutInflater inflater;

public CustomSpinnerAdapter(Context context, ArrayList<String> objects) {
super(context, R.layout.spinner_row, objects);

context1 = context;
data = objects;

inflater = (LayoutInflater) context1
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

// This funtion called for each row ( Called data.size() times )
public View getCustomView(int position, View convertView, ViewGroup parent) {

View row = inflater.inflate(R.layout.spinner_row, parent, false);

TextView tvCategory = (TextView) row.findViewById(R.id.tvCategory);

tvCategory.setText(data.get(position).toString());

return row;
}
}



spinner_row.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/spinner_selector"
android:orientation="vertical" >

<TextView
android:id="@+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="18sp"
/>

</RelativeLayout>


spinnerAcivity



public class SpinToolbarActivity extends AppCompatActivity {

private Toolbar toolbar;

private Spinner spinner_nav;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spintoolbaractivity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
spinner_nav = (Spinner) findViewById(R.id.spinner_nav);

if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);

}
addItemsToSpinner();

}

// add items into spinner dynamically
public void addItemsToSpinner() {

ArrayList<String> list = new ArrayList<String>();
list.add("Top News");
list.add("Politics");
list.add("Business");
list.add("Sports");
list.add("Movies");

// Custom ArrayAdapter with spinner item layout to set popup background

CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(
getApplicationContext(), list);
spinner_nav.setAdapter(spinAdapter);

spinner_nav.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(getApplicationContext(), "Selected : " + item,
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

最佳答案

       final ArrayList<String> lastPressed = new ArrayList<>();
final boolean[] isFirstTime = {true};
lastPressed.add(0,String.valueOf(0)); // last pressed position
lastPressed.add(1,"A");

...

spinner_nav.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
...
spinAdapter.remove(list.get(position));
if(!isFirstTime[0]){
spinAdapter.insert(lastPressed.get(1), new Integer(lastPressed.get(0)));
} else {
isFirstTime[0] = false;
}


String selected = ((TextView) view.findViewById(R.id.tv)).getText().toString();
lastPressed.set(0, String.valueOf(position));
lastPressed.set(1, selected);


spinAdapter.notifyDataSetChanged();

}

关于android - 如何从android中的微调器列表中隐藏所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43411260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com