出于某种原因,在styles.xml
中声明自定义drawable时,我的自定义drawable不会作为自定义快速滚动缩略图应用。我真的不明白为什么当这样的事情应该是直截了当时它不会改变。使用Theme.AppCompat.NoActionBar
主题时,如何使用自定义绘图用于快速滚动缩略图?
清单
<activity
android:name=".fruit.FruitActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
fragment_withfastscroll.xml
<?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:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:fastScrollEnabled="true"
android:scrollbarStyle="outsideInset" />
</LinearLayout>
styles.xml
<style name="OrangeTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:fastScrollPreviewBackgroundRight">@drawable/fastscrollpreview_orange</item>
<item name="android:fastScrollOverlayPosition">atThumb</item>
<item name="android:fastScrollThumbDrawable">@drawable/fastscrollthumb_orange</item>
<item name="android:fastScrollTextColor">#fff</item>
</style>
FruitListAdapter.java
public class FruitLineListAdapter extends BaseAdapter implements SectionIndexer {
private List<Fruit> mData;
private LayoutInflater mInflater;
private Object[] mSections;
private int[] mSectionsIndexedByPosition;
private int[] mPositionsIndexedBySection;
public FruitListAdapter(List<Fruit> data, Context context) {
mData = data;
mData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setupSections();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FruitListAdapter.ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem_dualline, parent, false);
holder = new FruitListAdapter.ViewHolder();
holder.title = convertView.findViewById(R.id.listitemtitle_singleline);
holder.description = convertView.findViewById(R.id.item_description);
convertView.setTag(holder);
} else {
holder = (FruitListAdapter.ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
private TextView title;
private TextView description;
}
@Override
public int getPositionForSection(int section) {
return mPositionsIndexedBySection[section];
}
@Override
public int getSectionForPosition(int position) {
return mSectionsIndexedByPosition[position];
}
@Override
public Object[] getSections() {
return mSections;
}
private void setupSections() {
String initial = "\0";
List<String> sections = new ArrayList<>();
mSectionsIndexedByPosition = new int[mData.size()];
mPositionsIndexedBySection = new int[mData.size()];
int section = 0;
for (int pos = 0; pos < mData.size(); pos++) {
Fruit fruit = mData.get(pos);
if (initial.charAt(0) != fruit.getStation().charAt(0)) {
initial = fruit.getStation().substring(0, 1);
section = sections.size();
sections.add(initial);
mPositionsIndexedBySection[section] = pos;
mSectionsIndexedByPosition[pos] = section;
} else {
mSectionsIndexedByPosition[pos] = section;
}
}
mSections = sections.toArray();
mPositionsIndexedBySection = Arrays.copyOf(mPositionsIndexedBySection, mSections.length);
}
}
片段
public class FragmentFruit extends ListFragment {
public FragmentFruit() {}
public static FragmentFruit newInstance() {
return new FragmentFruit();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_withfastscroll, container, false);
initialize();
return view;
}
List<Fruit> list = new ArrayList<>();
private void initialize() {
String[] items = getActivity().getResources().getStringArray(R.array.fruit_names);
String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.fruit_descriptions);
for (int n = 0; n < items.length; n++){
Fruit fruit = new Fruit();
fruit.setID();
fruit.setStation(items[n]);
fruit.setZone(itemDescriptions[n]);
list.add(fruit);
}
FruitListAdapter mAdapter = new FruitListAdapter(list, getActivity());
setListAdapter(mAdapter);
}
public static void ChangeColorScrollBar(View Scroll, int Color, Context cxt){
try
{
Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
mScrollCacheField.setAccessible(true);
Object mScrollCache = mScrollCacheField.get(Scroll);
Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(mScrollCache);
Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
method.setAccessible(true);
Drawable[] layers = new Drawable[1];
ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(cxt.getResources().getColor(Color));
sd1.setIntrinsicWidth(Math.round(cxt.getResources().getDimension(R.dimen.dp_3)));
layers[0] = sd1;
method.invoke(scrollBar, layers);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getView();
mTwoPane = getActivity().findViewById(R.id.detail_container) != null;
assert v != null;
ListView lv = v.findViewById(android.R.id.list);
lv.mScrollCache.scrollBar.setVerticalThumbDrawable(getResources().getDrawable(R.drawable.fastscrollthumb_orange));
super.onActivityCreated(savedInstanceState);
}
}
答案 0 :(得分:-1)
<style name="ListViewStyle.OrangeTheme" parent="android:style/Widget.ListView">
<item name="android:fastScrollPreviewBackgroundRight">@drawable/fastscrollpreview_orange</item>
<item name="android:fastScrollOverlayPosition">atThumb</item>
<item name="android:fastScrollThumbDrawable">@drawable/fastscrollthumb_orange</item>
<item name="android:fastScrollTextColor">#fff</item>
</style>
<?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:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:fastScrollEnabled="true"
android:scrollbarStyle="outsideInset"
style="@style/ListViewStyle.OrangeTheme"/>
</LinearLayout>