I have a problem with ListView, I need the checkboxes to go first on the layout, and then the list, the checkbox clicks filter the list.

layout.xml

<?xml version="1.0" encoding="utf-8"?> 

 <CheckBox android:id="@+id/prop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Пропущенные"/> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/prop" android:background="@drawable/frame" > <!-- the icon view --> <ImageView android:id="@+id/tvIcon" android:layout_width="160dp" android:layout_height="90dp" android:padding="5dp" android:scaleType="fitXY" android:layout_alignParentLeft="true" /> <!-- the container view for the title and description --> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/tvIcon" android:layout_centerVertical="true" > <!-- the title view --> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@android:style/TextAppearance.Medium" /> <!-- the timer view --> <Chronometer android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvChronometer" android:layout_below="@id/tvTitle" android:textAppearance="@android:style/TextAppearance.Medium" /> </RelativeLayout> </RelativeLayout> 

ArchiveFragment.class

 public class ArchiveFragment extends ListFragment { private List<ListViewItem> mItems; // ListView items list @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new DealsTask().execute((Void) null); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Удаляем разделители из ListView в ListFragment getListView().setDivider(null); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Получаем объект theListView ListViewItem item = mItems.get(position); String[] strArr = item.title.split("/"); // Делаем что-нибудь. Fragment fragment = null; Class fragmentClass = DetailFragment.class; try { fragment = (Fragment) fragmentClass.newInstance(); } catch (Exception e) { e.printStackTrace(); } Bundle bundle = new Bundle(); bundle.putString("suggestion_id", strArr[1]); bundle.putString("listActivity", "1"); assert fragment != null; fragment.setArguments(bundle); android.app.FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); ConstantsAPP.active_item = ConstantsAPP.ACTIVE_ORDER_DETAIL; } public class DealsTask extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... params) { return new APIweb(getActivity(), false).GetArchiveAll(); } @Override protected void onPostExecute(String strJson) { super.onPostExecute(strJson); if(strJson != null) { JSONObject dataJsonObj; String suggestion_aссepted = null; try { dataJsonObj = new JSONObject(strJson); JSONArray js_arr = dataJsonObj.getJSONArray("result"); JSONObject error = js_arr.getJSONObject(0); if (parseInt(error.getString("error")) == 0) { // initialize the items list mItems = new ArrayList<ListViewItem>(); JSONArray js_arr_suggestion = dataJsonObj.getJSONArray("suggestion"); for (int i = 0; i < js_arr_suggestion.length(); i++) { JSONObject obj_suggestion = js_arr_suggestion.getJSONObject(i); String suggestion_id = obj_suggestion.getString("suggestion_id"); suggestion_aссepted = obj_suggestion.getString("suggestion_aссepted"); String d_id = obj_suggestion.getString("d_id"); String d_type = obj_suggestion.getString("d_type"); String d_created_at = obj_suggestion.getString("d_created_at"); Drawable img = getResources().getDrawable(R.drawable.defaultwork); mItems.add(new ListViewItem(img, d_id + "/" + suggestion_id + "/" + d_type, d_created_at, null, null, null, suggestion_aсcepted)); } // initialize and set the list adapter setListAdapter(new ListViewAdapter(getActivity(), mItems, R.layout.fragment_archive_listview)); } else if(parseInt(error.getString("error")) == 1) { setEmptyText("Нет активных заявок."); mItems = new ArrayList<ListViewItem>(); setListAdapter(new ListViewAdapter(getActivity(), mItems, R.layout.fragment_archive_listview)); } } catch (JSONException e) { e.printStackTrace(); } } } } 

}

But it turns out such sadness: enter image description here

  • And then how? - katso
  • What would be the top checkbox and the bottom of the list. I originally wrote that. Not every item has a checkbox, but a checkbox only on top of the layout. - RAPOS
  • Add CheckBox as Header - Vladyslav Matviienko
  • I did it already, but I can't fix the checkbox in the top. I need the checkbox not to scroll. - RAPOS
  • So do not add it to the list itself, but simply add a separate View to the markup above the list. - Vladyslav Matviienko

1 answer 1

Your problem is that you use a ListFragment , which by default in the OnCreateView() method adds a ListView , you first need to override this method like this:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.archive_fragment, container, false); } 

Secondly write archive_fragment.xml , like this:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

Notice the id the ListView , it should be @android:id/list if you want to continue using the ListFragment .

Third, remove the checkbox from item 'a.

  • Thank you very much, the checkbox is no longer in the item - RAPOS