Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何自定义ListView上的滚动条?_Android_Android Listview_Scrollbar - Fatal编程技术网

Android 如何自定义ListView上的滚动条?

Android 如何自定义ListView上的滚动条?,android,android-listview,scrollbar,Android,Android Listview,Scrollbar,注意:在你说这是一个重复,其他人已经解决了它,还有一些教程(比如)之前,我知道,但由于某些原因,它们不适合我 背景 我制作了一个库(),展示了如何模仿棒棒糖联系人应用程序显示左侧标题的方式 我想知道如何定制滚动条和快速滚动条,使其看起来像棒棒糖(或者像我所做的那样像联系人应用程序,或者像ListView的默认应用程序) 问题 出于某种原因,尽管我使用的属性与所有教程和StackOverflow文章中讨论的属性相同,但它们没有任何作用 以下是我创建的样式: <style name="Cust

注意:在你说这是一个重复,其他人已经解决了它,还有一些教程(比如)之前,我知道,但由于某些原因,它们不适合我

背景 我制作了一个库(),展示了如何模仿棒棒糖联系人应用程序显示左侧标题的方式

我想知道如何定制滚动条和快速滚动条,使其看起来像棒棒糖(或者像我所做的那样像联系人应用程序,或者像ListView的默认应用程序)

问题 出于某种原因,尽管我使用的属性与所有教程和StackOverflow文章中讨论的属性相同,但它们没有任何作用

以下是我创建的样式:

<style name="CustomTheme" >
  <item name="android:scrollbarFadeDuration">250</item>
  <item name="android:scrollbarDefaultDelayBeforeFade">300</item>
  <item name="android:scrollbarSize">10dip</item>
  <item name="android:scrollbarThumbHorizontal">@drawable/scrollbar_handle_material</item>
  <item name="android:scrollbarThumbVertical">@drawable/scrollbar_handle_material</item>
  <item name="android:scrollbarTrackHorizontal">@null</item>
  <item name="android:scrollbarTrackVertical">@null</item>
</style>
我试过的 我尝试使用属性,尝试使用简单的ListView(和GridView,通过使用示例“”),并尝试在ListView标记中设置正确的属性(而不是使用样式),如下所示:

。。。但是什么也做不了

问题 怎么可能呢?少了什么?怎么了


我还想问一下,在Android 2.x上是否可以这样做。

环顾四周时,我在这里遇到了你的问题。果然,本周我遇到了同样的问题(涉及自定义滚动条)。我提出了一个解决方案,它没有那么难看,但另一方面,它是高度可定制的,应该可以在所有设备上工作,而不考虑布局问题,所以这是一个加号

我制作了一个外部自定义滚动条容器(类似PC,带有曲目背景等),如下所示:

我在scrollView中使用了这段代码,因此您必须以像素为单位计算列表长度

要使滚动条可拖动:添加类范围变量
private double scrollstore=0然后:

    findViewById(R.id.scrollbar).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v,MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    scrollstore = ((FrameLayout.LayoutParams)v.getLayoutParams()).topMargin - event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)v.getLayoutParams();
                    double thumbpos = event.getRawY()+scrollstore;
                    double scrlboxh = findViewById(R.id.scrollbarcontainer).getHeight();
                    if(thumbpos<0) thumbpos = 0;
                    if(thumbpos>scrlboxh-params.height) thumbpos = scrlboxh-params.height;
                    params.topMargin = (int)Math.round(thumbpos);
                    v.setLayoutParams(params);
                    double ttllen = (double)***LIST_LENGTH_IN_PIXELS***;
                    double calc = ttllen * (thumbpos/scrlboxh);
                    findViewById(R.id.scrollView).scrollTo(findViewById(R.id.scrollView).getScrollX(),(int)Math.round(calc));
                    break;
            }
            return true;
        }
    });
findviewbyd(R.id.scrollbar).setOnTouchListener(新视图.OnTouchListener()){
@凌驾
公共布尔onTouch(视图v,运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
scrollstore=((FrameLayout.LayoutParams)v.getLayoutParams()).topMargin-event.getRawY();
打破
case MotionEvent.ACTION\u移动:
FrameLayout.LayoutParams params=(FrameLayout.LayoutParams)v.getLayoutParams();
double thumbpos=event.getRawY()+滚动存储;
double-scrlboxh=findviewbyd(R.id.scrollbarcontainer).getHeight();
如果(thumbposcrlboxh参数高度)thumbpos=scrlboxh-params.height;
params.topMargin=(int)数学四舍五入(thumbpos);
v、 setLayoutParams(参数);
双ttllen=(双)***以像素为单位的列表长度***;
双计算=ttllen*(thumbpos/scrlboxh);
findViewById(R.id.scrollView).scrollTo(findViewById(R.id.scrollView).getScrollX(),(int)Math.round(calc));
打破
}
返回true;
}
});

快乐编码,

你能将
parent=“@android:style/Widget.ListView”
添加到
CustomTheme
?@Blackbelt正在尝试。没有帮助。此外,正如我所提到的,我甚至尝试过不声明新样式,也不直接放置属性:(你能在其他设备上试用吗?@Blackbelt我在多个版本的模拟器上试用过。在我的设备上试用是没有用的,因为它已经有棒棒糖5.1。这看起来与我为RecyclerView找到的解决方案类似(在我写过的库中)。难道没有一个官方的方法来定制滚动条和快速滚动条吗?为什么代码没有像我写的那样工作?还有,你能发布拖拽代码吗?另外,你知道我应该从Android源代码复制的正确资源在哪里吗,以使它看起来一样(除非我发现的东西是正确的)?当人们在这里报道时,XML方法在复杂的布局中非常有缺陷,人们说拇指卡在视图下面等等…还有,主题应该如何显示,真的要由制造商来决定(想想三星和HTC),我不会依赖这些属性。所以基本上是“官方的”方式是不可靠的。android资源只是一个不透明度的背景色(不透明度为0.5,大约为#000),通过“官方方式”,我指的是使用API。关于android资源,这不正是我所发现的吗(尽管出于某种原因,它看起来只是“拇指”,而不是它背后的东西)?
style="@style/CustomTheme"
            android:scrollbarFadeDuration="250"
            android:scrollbarDefaultDelayBeforeFade="300"
            android:scrollbarSize="10dp"
            android:scrollbarThumbHorizontal="@drawable/scrollbar_handle_material"
            android:scrollbarThumbVertical="@drawable/scrollbar_handle_material"
            android:scrollbarTrackHorizontal="@null"
            android:scrollbarTrackVertical="@null"
<FrameLayout //for single-child, absolute placement (margin pixels)
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/listView" //I placed scrollbar aside my container
    android:id="@+id/scrollbarcontainer"
    android:background="@drawable/scrollbg">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollbar"
        android:src="@drawable/scrollbarthumb"
        android:scaleType="fitXY"/> //thumb is stretched to indicate list length
</FrameLayout>
    findViewById(R.id.listView).setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view,int scrollState) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)findViewById(R.id.scrollbar).getLayoutParams();

            //proportional math, will match exactly to default scroll thumb
            double scrlboxh = (double)findViewById(R.id.scrollbarcontainer).getHeight();
            double ttllen = (double)***LIST_LENGTH_IN_PIXELS***;
            double scrlpos = (double)view.getScrollY();
            double boxh = (double)findViewById(R.id.listView).getHeight(); //layout height on screen

            double calc = scrlboxh / (ttllen/boxh);

            params.height = (int)Math.round(calc);

            calc = scrlboxh / (ttllen/scrlpos);

            params.topMargin = (int)Math.round(calc);

            findViewById(R.id.scrollbar).setLayoutParams(params);
        }
        @Override
        public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount) {
        }
    });
    findViewById(R.id.scrollbar).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v,MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    scrollstore = ((FrameLayout.LayoutParams)v.getLayoutParams()).topMargin - event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)v.getLayoutParams();
                    double thumbpos = event.getRawY()+scrollstore;
                    double scrlboxh = findViewById(R.id.scrollbarcontainer).getHeight();
                    if(thumbpos<0) thumbpos = 0;
                    if(thumbpos>scrlboxh-params.height) thumbpos = scrlboxh-params.height;
                    params.topMargin = (int)Math.round(thumbpos);
                    v.setLayoutParams(params);
                    double ttllen = (double)***LIST_LENGTH_IN_PIXELS***;
                    double calc = ttllen * (thumbpos/scrlboxh);
                    findViewById(R.id.scrollView).scrollTo(findViewById(R.id.scrollView).getScrollX(),(int)Math.round(calc));
                    break;
            }
            return true;
        }
    });