導航:首頁 > 操作系統 > androidlistview橫向滑動

androidlistview橫向滑動

發布時間:2022-07-12 12:20:26

『壹』 android listview怎麼左右滑動

提供點偽代碼給你看看

class News {
int id; // 新聞標識
String summary; // 概述
String imageUrl; // 圖片資源鏈接
}

class NewsDetail {
int id; // 新聞標識
String summary; // 概述
String detail; // 詳細
}
我們的ListView對應的Adapter裡面引用的數據源就是
ArrayList<News>列表。 可以把這個列表緩存在Application里做一個全局變數。

然後你點擊進入一個新聞詳情

OnItemClickListener(new ...() {

void onItemClick() {
Intent i = new Intent(context, NewsDetailActivity.class);
i.putExtra("id", id);
startActivity(i);
}
});
NewsDetailActivity是簡單的新聞詳情頁面
裡面主要的控制項是ViewPager,他的數據源是根據之前的NewsList而來的。 ArrayList<NewsDetail> detailList = new ArrayList<NewsDetail>();

onCreate 方法裡面最後通過一個非同步方式去獲取新聞的詳情,然後顯示出來。

class NewsDetailTask extends AsyncTask<String, Void, Void> {
onBackground() {
// 緩存裡面獲取新聞詳情或者
// http get/post("...");獲取新聞詳情
// 顯示出來
}
}
設置OnPageChangeListener,在onPageChanged方法裡面去調整當前頁面展示的新聞詳情

class MyOnPageChangeListener implements OnPageChangeListener {
void onPageSelected(int position) {

int id = ((XXApplication) getApplication()).getNewsList().get(position).getId();
new NewsDetailTask("" + id);
}
}
就可以了。

『貳』 怎麼解決Android listview 單個item的左右滑動和整個listview上下滑動的沖突問題

你是listView把滑動事件截取了,然後item就不能響應滑動了嗎?這樣可以重寫listView,重寫onInterceptTouchEvent()

『叄』 android studio listview怎麼滾動

按下按鈕會觸發ListView滾動或停止。

實現該功能並不難,下面給出主要代碼MainActivity.java

?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 package cn.guet.levide;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {
private Button btn_up, btn_down, btn_stop; // 三個按鈕
private ListView listview;
private Adapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findBy();
init();
}

private void init() {
btn_up.setOnClickListener(this);
btn_down.setOnClickListener(this);
btn_stop.setOnClickListener(this);

adapter = new Adapter(this);
listview.setAdapter(adapter);
}

private void findBy() {
btn_up = (Button) findViewById(R.id.btn_scroll_up);
btn_down = (Button) findViewById(R.id.btn_scroll_down);
btn_stop = (Button) findViewById(R.id.btn_scroll_stop);

listview = (ListView) findViewById(R.id.listview);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_scroll_down:
listScrollDown();
break;
case R.id.btn_scroll_up:
listScrollUp();
break;
case R.id.btn_scroll_stop:
listScrollOff();
break;
}
}

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
handler.removeCallbacks(run_scroll_down);
handler.removeCallbacks(run_scroll_up);
}
};

/**
* 向上滾動
*/
public void listScrollUp() {
listScrollOff();
handler.postDelayed(run_scroll_up, 0);
}

/**
* 向下滾動
*/
public void listScrollDown() {
listScrollOff();
handler.postDelayed(run_scroll_down, 0);
}

/**
* 停止滾動
*/
public void listScrollOff() {
handler.removeCallbacks(run_scroll_down);
handler.removeCallbacks(run_scroll_up);
}

Runnable run_scroll_up = new Runnable() {
@Override
public void run() {
/**
* public void smoothScrollBy (int distance, int ration)
*
* Added in API level 8 Smoothly scroll by distance pixels over ration milliseconds.
*
* Parameters
* distance Distance to scroll in pixels.
* ration Duration of the scroll animation in milliseconds.
*/
listview.smoothScrollBy(1, 10);
handler.postDelayed(run_scroll_up, 10);
}
};
Runnable run_scroll_down = new Runnable() {
@Override
public void run() {
listview.smoothScrollBy(-1, 10);
handler.postDelayed(run_scroll_down, 10);
}
};
}

實現ListView位置變動的是smoothScrollBy方法。

?1
2
3
4
5
6 public void smoothScrollBy (int distance, int ration)
Smoothly scroll by distance pixels over ration milliseconds.

Parameters
distance Distance to scroll in pixels.
ration Duration of the scroll animation in milliseconds.

工程源碼

Android listview 代碼控制上下移動

『肆』 Android ListView列表如何橫向滾動

如果在你的源碼基礎上改好像不太可能,你把textview改成多行顯示吧

『伍』 android 如何給listview添加橫向滾動條,網上說的在listview外層加horizontalscrollview沒有用啊

誰說沒用,親測可以

xml布局裡寫:

<HorizontalScrollView

android:layout_width="200dp"

android:layout_height="fill_parent">

<ListView

android:layout_width="400dp"

android:layout_height="fill_parent"

android:footerDividersEnabled="false"

android:headerDividersEnabled="false"

android:scrollingCache="false"

/>

</HorizontalScrollView>

一個橫向滾動且縱向滾動的listView不就出來了

『陸』 android 怎麼讓listview橫著

CSDN論壇去看看。。
Dim lvhti As LVHITTESTINFO
Call GetCursorPos(lvhti.pt)
Call ScreenToClient(listview1.hwnd, lvhti.pt)
Call SendMessage(listview1.hwnd, LVM_SUBITEMHITTEST, 0, lvhti.pt)

i = lvhti.iItem
If i > = 0 Then
i=i+1 '這時候的i就是點的行數
end if
以上代碼寫在雙擊事件中,
其中用的到API函數和常量,你自己查資料。

『柒』 android listview為什麼不能滑動

  1. 你在listview上無意中加了一層view,使listview無法接收滑動事件

  2. 該listview在scrollview上時,是無法獲取滑動事件,只會被scrollview攔截。

  3. listview中數據不夠多,沒有超越屏幕的存在,自然不能滑動。

『捌』 android listview如何按時加滾動到頂部

listview的一些方法:

1.public
void setSelection (int position)

position位置的item置頂(即position位置的item到listview的最頂端),但是,沒有從原來位置到頂端的滑動效果。

2.public
void setSelectionFromTop (int position, int y)

與setSelection 類似。setSelection(position)就是setSelectionFromTop (position,0)

選中position指定的項目,並將所選項置於距離ListView頂端y像素的位置

參數

position 需要選中的項目的索引(從0開始)

y
距離ListView(包括間隙)頂端的位置

3.public
void smoothScrollToPositionFromTop (int position, int
offset)

平滑滾動到指定的適配器位置。 指定位置的視圖會滾動到相對頂邊偏移 offset 像素的位置顯示。
如果無法做到(比如該偏移量會使首尾條目超越列表邊緣),會滾動到盡量接近的位置。

參數

position 滾動到的位置

offset 滾動結束時,指定 position 條目距離視圖頂部的像素數

當offset=0時,可以把position位置的item置頂,而且帶有滑動效果,即會從原來位置輕輕滑動到最頂

端位置。

測試:
可以順利滾動,返回滾動也很順利,滑動順暢

4、public void
smoothScrollToPositionFromTop(int position)

參數:

position 滾動到的位置

帶有滑動效果的滾動到指定位置。

如何判斷其內容已滾動到最頂部或者最底部看

getListView().setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem==0){
Log.e("log", "滑到頂部");
}
if(visibleItemCount+firstVisibleItem==totalItemCount){
Log.e("log", "滑到底部");
}
}
});

平滑的滾動listview到一個指定位

package com.android.contacts.widget;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.ListView;

public class AutoScrollListView extends ListView {

private static final
float PREFERRED_SELECTION_OFFSET_FROM_TOP = 0.33f;

private int
mRequestedScrollPosition = -1;

private boolean
mSmoothScrollRequested;

public
AutoScrollListView(Context context) {

super(context);

}

public
AutoScrollListView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public
AutoScrollListView(Context context, AttributeSet attrs, int
defStyle) {

super(context, attrs, defStyle);

}

public void
requestPositionToScreen(int position, boolean smoothScroll) {

mRequestedScrollPosition = position;

mSmoothScrollRequested = smoothScroll;

requestLayout();

}

@Override

protected void
layoutChildren() {

super.layoutChildren();

if (mRequestedScrollPosition == -1) {

return;

}

final int position =
mRequestedScrollPosition;

mRequestedScrollPosition = -1;

int firstPosition = getFirstVisiblePosition() +
1;

int lastPosition =
getLastVisiblePosition();

if (position >= firstPosition &&
position <= lastPosition) {

return; //
Already on screen

}

final int offset = (int) (getHeight() *
PREFERRED_SELECTION_OFFSET_FROM_TOP);

if (!mSmoothScrollRequested) {

setSelectionFromTop(position, offset);

// Since
we have changed the scrolling position, we need to redo child
layout

// Calling
"requestLayout" in the middle of a layout pass has no effect,

// so we
call layoutChildren explicitly

super.layoutChildren();

} else {

// We will
first position the list a couple of screens before or after

// the new
selection and then scroll smoothly to it.

int
twoScreens = (lastPosition - firstPosition) * 2;

int
preliminaryPosition;

if
(position < firstPosition) {

preliminaryPosition = position + twoScreens;

if (preliminaryPosition >=
getCount()) {

preliminaryPosition = getCount() - 1;

}

if (preliminaryPosition <
firstPosition) {

setSelection(preliminaryPosition);

super.layoutChildren();

}

} else
{

preliminaryPosition =
position - twoScreens;

if (preliminaryPosition <
0) {

preliminaryPosition = 0;

}

if
(preliminaryPosition > lastPosition) {

setSelection(preliminaryPosition);

super.layoutChildren();

}

}

smoothScrollToPositionFromTop(position, offset);

}

}

}

『玖』 android HorizontalScrollView, 橫向滾動菜單,點擊項,控制移動的距離

有好多實現方法,簡單的就是用GridView(安卓自帶網格布局)或者HorizontalListView(自定義橫向list,網上有好多的)
再就是在activity中ScrollView自定義添加子View
還可以用ViewPager,TabHost

閱讀全文

與androidlistview橫向滑動相關的資料

熱點內容
噴油螺桿製冷壓縮機 瀏覽:577
python員工信息登記表 瀏覽:375
高中美術pdf 瀏覽:158
java實現排列 瀏覽:511
javavector的用法 瀏覽:979
osi實現加密的三層 瀏覽:230
大眾寶來原廠中控如何安裝app 瀏覽:910
linux內核根文件系統 瀏覽:240
3d的命令面板不見了 瀏覽:521
武漢理工大學伺服器ip地址 瀏覽:144
亞馬遜雲伺服器登錄 瀏覽:521
安卓手機如何進行文件處理 瀏覽:69
mysql執行系統命令 瀏覽:926
php支持curlhttps 瀏覽:142
新預演算法責任 瀏覽:443
伺服器如何處理5萬人同時在線 瀏覽:247
哈夫曼編碼數據壓縮 瀏覽:424
鎖定伺服器是什麼意思 瀏覽:383
場景檢測演算法 瀏覽:616
解壓手機軟體觸屏 瀏覽:348