ListView doesn't get styled with theme when using custom adapter

六月ゝ 毕业季﹏ 提交于 2021-01-28 18:00:32

问题


I have minidemo with three fragments. One displays two TextView and an editview directly, the next treat the same layout as rows in a list with custom adapter, the third displays a plain list with standard adapter (simple_list_item_1)

The activity uses a standard theme (Theme.AppCompat.Light.DarkActionBar), which I have not modified.

I would have expected all three blocks to be styled similarly. Instead, the second block with the custom adapter does not seem to get styled according to the light theme at all. (See screenshot at end of this post)

Ideal,ly I'd like the rows of the custom list to be styled like the first block. Alternatively, I'd like to get the style of the third block applied. However, all of the fonts and backgrounds seem to be some off color and I am not sure how to find out what is currently used as part of the theme. Therefore, fixing the middle block's style by manually assigning text colors, sizes, backgrounds, etc. is not an option.

For the whole demo please refer to MiniDemo

This is the item_row.xml layout file (The top level relative layout tag seems to be stripped by the editor. For the full layout please go to gitlab)

<TextView android:text="large"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceLarge"
    android:id="@+id/text1_field" />

<TextView android:text="large inverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text2_field"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="5"
    android:id="@+id/edit_field"
    android:layout_alignParentRight="true" />
</RelativeLayout>

The custom fragment. Again, the full version is on gitlab

public class CustomItemFragment extends ListFragment {
String[] DATA = new String[] {"foo1", "foo2", "foo3"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ItemAdapter adapter = new ItemAdapter(this.getActivity().getApplicationContext(), DATA);
    setListAdapter(adapter);
}

}`

The adapter (gitlab):

public class ItemAdapter extends BaseAdapter {
private static final String TAG = ItemAdapter.class.getSimpleName();

private final Context mContext;
private final String[] mData;

public ItemAdapter(Context mContext, String[] data) {
    this.mContext = mContext;
    this.mData = data;
}

@Override
public int getCount() {
    return mData.length;
}

@Override
public Object getItem(int position) {
    return mData[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(mContext);
    View rowView = inflater.inflate(R.layout.item_row, parent, false);

    TextView text1View = (TextView) rowView.findViewById(R.id.text1_field);
    TextView text2View = (TextView) rowView.findViewById(R.id.text2_field);
    EditText editText = (EditText) rowView.findViewById(R.id.edit_field);

    text1View.setText(mData[position]);
    text2View.setText(mData[position]);
    editText.setText(Integer.toString(position));

    return rowView;
}

And then, here is a sceenshot to demo the effect.

enter image description here


回答1:


Instead of using getApplicationContext(), use getBaseContext() or getContext()

More here : Theme/Style is not applied when inflater used with ApplicationContext




回答2:


Arghh, I just found the answer, with a hint from Use android.R.layout.simple_list_item_1 with a light theme

The trick is to remove getActivityContext() and to pass in the activity object directly when creating the adapter. Sad but effective.



来源:https://stackoverflow.com/questions/31546585/listview-doesnt-get-styled-with-theme-when-using-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!