Trong Android ListView
, làm thế nào tôi có thể làm cho scrollbar
xuất hiện ở phía bên trái?Làm thế nào để làm cho 'thanh cuộn' xuất hiện ở phía bên trái?
Trả lời
dụ:
mView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);
Bạn có thể di chuyển vị trí thanh cuộn sang bên trái cho bất kỳ Chế độ xem nào bằng cách sử dụng View.SCROLLBAR_POSITION_LEFT.
Khi hai câu trả lời khác đã đề cập, một trong những khả năng được sử dụng View.setVerticalScrollbarPosition() với SCROLLBAR_POSITION_LEFT. Tuy nhiên, một báo trước khổng lồ là điều này đòi hỏi cấp API 11+, mà tại thời điểm viết tài khoản cho ít hơn 10% cài đặt Android. Đối với hầu hết các ứng dụng, điều đó không thể chấp nhận được. Một trong những khả năng đến với tâm trí để thực hiện những gì bạn muốn trên các phiên bản Android cũ hơn sẽ làm điều gì đó rất kludgy: tắt thanh cuộn, phản chiếu bố cục chính của bạn với một bố trí hẹp bên trái của nó, chỉ rộng đủ để vừa với thanh cuộn và cuộn theo cách thủ công chế độ xem trái với scrollyBy() khi chế độ xem chính của bạn được cuộn (bằng cách ghi đè onScrollChanged()).
Điều đó nói rằng, tôi sẽ không thực sự khuyên rằng trừ khi có một lý do rất thuyết phục để di chuyển thanh cuộn sang bên trái. Trong hầu hết các trường hợp, bạn muốn ứng dụng của mình phù hợp và hoạt động giống như bất kỳ ứng dụng nào khác trên thiết bị bằng cách chỉ cho phép Android tuân theo các giá trị mặc định của ứng dụng đó.
Hãy thử hack của tôi, dường như hoạt động ít nhất là từ 2.2 trở lên.
import java.lang.reflect.Field;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
/**
* This class fixes the lack of setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT) before API level 11
* @author Genadz Batsyan
*/
public class ListViewWithLeftScrollBar extends ListView {
private static final String LOG_TAG = ListViewWithLeftScrollBar.class.getSimpleName();
private static final boolean DEBUG = true;
private boolean patchInvalidate;
public ListViewWithLeftScrollBar(Context context) {
super(context);
moveVerticalScrollbarToTheLeft();
}
public ListViewWithLeftScrollBar(Context context, AttributeSet attrs) {
super(context, attrs);
moveVerticalScrollbarToTheLeft();
}
public ListViewWithLeftScrollBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
moveVerticalScrollbarToTheLeft();
}
@Override
public void invalidate(Rect r) {
invalidate(r.left, r.top, r.right, r.bottom);
}
@Override
public void invalidate(int left, int top, int right, int bottom) {
int width = right - left;
if (DEBUG) log("invalidate original w:"+ getWidth() +" h:"+ getHeight()+" rect:"+left+", "+top+", "+right+", "+bottom);
if (patchInvalidate && right == getWidth() && top == 0 && bottom == getHeight() && width < 30) {
// The above condition should ensure that ListView is VERY likely to be invalidating the scrollbar.
// In fact ListView appears to not invalidate anything except the scrollbar, ever.
left = 0;
right = left + width;
if (DEBUG) log("invalidate patched w:"+ getWidth() +" h:"+ getHeight()+" rect:"+left+", "+top+", "+right+", "+bottom);
}
super.invalidate(left, top, right, bottom);
}
private void moveVerticalScrollbarToTheLeft() {
try {
if (DEBUG) log("moveVerticalScrollbarToTheLeft: Trying API Level >=11");
tryApiLevel11();
if (DEBUG) log("moveVerticalScrollbarToTheLeft: API Level >=11 success");
} catch (Throwable t1) {
if (DEBUG) {
log("moveVerticalScrollbarToTheLeft: API Level >=11 FAILED");
t1.printStackTrace();
}
try {
if (DEBUG) log("moveVerticalScrollbarToTheLeft: Trying hack for API Level <11");
tryApiLevelPre11();
patchInvalidate = true;
if (DEBUG) log("moveVerticalScrollbarToTheLeft: API Level <11 hack success");
} catch (Throwable t2) {
if (DEBUG) {
log("moveVerticalScrollbarToTheLeft: API Level <11 hack FAILED");
t2.printStackTrace();
}
}
}
}
@SuppressLint("NewApi")
private void tryApiLevel11() {
setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);
}
private void tryApiLevelPre11() throws Exception {
Class<?> viewClass = View.class;
Field scrollCacheField = viewClass.getDeclaredField("mScrollCache");
scrollCacheField.setAccessible(true);
Object scrollCache = scrollCacheField.get(this);
if (DEBUG) log("scrollCache: "+ scrollCache);
Field scrollBarField = scrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(scrollCache);
if (DEBUG) log("scrollBar: "+ scrollBar);
Field verticalThumbField = scrollBar.getClass().getDeclaredField("mVerticalThumb");
verticalThumbField.setAccessible(true);
Object verticalThumb = verticalThumbField.get(scrollBar);
if (DEBUG) log("verticalThumb: "+ verticalThumb);
Drawable verticalThumbDrawable = (Drawable) verticalThumb;
Drawable replacementVerticalThumbDrawable = new LayerDrawable(new Drawable[]{ verticalThumbDrawable }) {
@Override
public void setBounds(int left, int top, int right, int bottom) {
if (DEBUG) log("setBounds original: "+left+", "+top+", "+right+", "+bottom);
int width = right - left;
left = 0;
right = left + width;
if (DEBUG) log("setBounds patched: "+left+", "+top+", "+right+", "+bottom);
super.setBounds(left, top, right, bottom);
}
};
verticalThumbField.set(scrollBar, replacementVerticalThumbDrawable);
}
private static void log(String msg) {
Log.d(LOG_TAG, msg);
}
}
Tôi đã thử nó trên 2.2.1 và nó không hoạt động. –
Vâng, nó hoạt động nếu tôi gọi 'tryApiLevelPre11()' trong cơ thể 'if (Build.VERSION.SDK_INT
- 1. Làm thế nào để định vị thanh cuộn div ở phía bên tay trái?
- 2. Extjs làm thế nào để làm cho thanh cuộn xuất hiện?
- 3. DIV Thanh cuộn dọc trên bên trái
- 4. Làm thế nào để tạo bóng hộp chỉ ở bên trái, bên phải và dưới cùng?
- 5. Làm thế nào để giữ hình nền ở dưới cùng bên trái thậm chí di chuyển
- 6. Làm thế nào để biết nếu thanh cuộn đã xuất hiện trong trình duyệt (jQuery)?
- 7. Làm cách nào để căn chỉnh các mục trong thanh tác vụ ở bên trái?
- 8. JTabbedPane: biểu tượng ở phía bên trái của các tab
- 9. Magento: Làm cách nào để hiển thị mô tả danh mục ở thanh bên trái?
- 10. Làm thế nào tôi có thể làm cho đánh dấu bằng không xuất hiện ở góc bên trái trong các ô R?
- 11. Đặt dấu kiểm ở phía bên trái của UITableViewCell
- 12. Sử dụng DataTemplate cho MenuItem khiến không gian thừa ở bên trái xuất hiện?
- 13. Các bản vá hiện có cho tabwidget của Android để cho phép các tab ở phía bên trái?
- 14. Làm thế nào tôi có thể làm cho các widget tràn để làm cho một thanh cuộn xuất hiện trong Qt?
- 15. Emacs: chỉ số TODO ở phía bên trái
- 16. Box-Shadow ở phía bên trái của phần tử chỉ
- 17. Thanh màu xám ở bên trái trong vim?
- 18. Làm cách nào để đảm bảo rằng trang web mở ra bằng thanh cuộn ở giữa
- 19. Làm cách nào để có thanh cuộn xuất hiện trong thành phần bìa Rally?
- 20. Làm thế nào để xác định đột quỵ hình chữ nhật chỉ ở bên trái và bên phải trong WPF?
- 21. Cuộn bên trong thanh bên cố định
- 22. Làm thế nào để đặt Drawound hợp chất ở góc trên bên trái của Text View
- 23. Làm thế nào để bù cho thanh cuộn dọc khi nó chưa có mặt
- 24. Làm cách nào để tạo thanh cuộn html ở dưới cùng?
- 25. Cách tốt nhất của Android để làm cho cuộn xuất hiện để quay
- 26. Làm thế nào để tạo thanh cuộn jQuery tùy chỉnh
- 27. html cellpadding phía bên trái của ô
- 28. Làm thế nào để tắt thanh cuộn trong ScrolledForm?
- 29. Làm thế nào để tắt thanh cuộn ngang trong FlowLayoutPanel?
- 30. Làm thế nào để bạn thực hiện một div theo sau khi bạn cuộn?
nó cũng có thể thông qua xml: android: verticalScrollbarPosition = "left" –
Đáng buồn là không làm việc trong [NavigationView] (http://developer.android.com/intl/ru/reference/android/support/design/ widget/NavigationView.html) (API 23). – Luten