如何构建两个具有动态内容的同步ListView
How to build Two Synchronous ListView with Dynamic Contents
我想创建一个 ScrollView
,它的动态内容本身有两列。
并且右栏的项目连接到交叉定位的左栏的项目。 (比如第一列项目 y 坐标:[(0,100),(100,200),(200,300)] -- 第二列项目 y 坐标:[(50,150),(150,250)])
使用一个 ScrollView
或一个 ListView
是不可能的,因为据我所知,它们不能像那样交叉放置两列。所以,我有两个解决方案。这些解决方法有不同的问题。
这是我的第一个解决方案:
-ScrollView 中的动态自定义视图。
- 这种方法的问题是我不知道如何更新 ScrollView
和动态自定义视图,所以它不会滚动。
我的第二种方案:
-两个独立连接的列表视图(如果你滑动一个,另一个会自动同步滑动到相同位置)。
-这个解决方案的问题是我找不到将它们相互连接的方法。
我们将不胜感激。
对于任何可能需要答案的人,我在 here. I improved this with that 中找到了解决方案(这是不正确的)(实际上我已将其修复)。这是我的最终解决方案:
MyScrollView.java
public class MyScrollView extends ScrollView implements IScrollListener {
private static final String TAG = "MyScrollView";
private IScrollListener listener;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setScrollViewListener(IScrollListener listener) {
this.listener = listener;
}
@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (listener != null) {
listener.onScrollChanged(this, x, y, oldX, oldY);
} else {
Log.i(TAG, "asdasd");
}
}
@Override
public void onScrollChanged(IScrollListener scrollView, int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (listener != null) {
listener.onScrollChanged(this, x, y, oldX, oldY);
} else {
Log.i(TAG, "asdasd");
}
}
}
MyActivity.java
public class MyActivity extends AppCompatActivity implements IScrollListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_position);
.
.
.
scrollViewLeft = (MyScrollView) this.findViewById(R.id.scrollViewLeft);
scrollViewRight = (MyScrollView) this.findViewById(R.id.scrollViewRight);
.
.
scrollViewLeft.setScrollViewListener(this);
scrollViewRight.setScrollViewListener(this);
.
.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLeft = vi.inflate(R.layout.target_item, null);
View viewRight = vi.inflate(R.layout.target_item_right, null);
// insert into left view
ViewGroup insertPointLeft = (ViewGroup) findViewById(R.id.leftLinearLayout);
insertPointLeft.addView(viewLeft, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// insert into right view
ViewGroup insertPointRight = (ViewGroup) findViewById(R.id.rightLinearLayout);
insertPointRight.addView(viewRight, targets.size(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// fill in any details dynamically here
TextView number = (TextView) viewLeft.findViewById(R.id.number);
number.setText("1");
TextView name = (TextView) viewLeft.findViewById(R.id.name);
name.setText("Muhammed Aydogan");
// fill in any details dynamically here
TextView number2 = (TextView) viewRight.findViewById(R.id.number);
number2.setText("2");
TextView name2 = (TextView) viewRight.findViewById(R.id.name);
name2.setText("Muhammed Aydogan");*/
}
@Override
public void onScrollChanged(IScrollListener scrollView, int x, int y, int oldX, int oldY) {
if (scrollViewLeft.equals(scrollView)) {
scrollViewRight.scrollTo(x, y);
} else if (scrollViewRight.equals(scrollView)) {
scrollViewLeft.scrollTo(x, y);
}
}
}
IScrollListener.java
public interface IScrollListener {
void onScrollChanged(IScrollListener scrollView, int x, int y, int oldX, int oldY);
}
我想创建一个 ScrollView
,它的动态内容本身有两列。
并且右栏的项目连接到交叉定位的左栏的项目。 (比如第一列项目 y 坐标:[(0,100),(100,200),(200,300)] -- 第二列项目 y 坐标:[(50,150),(150,250)])
使用一个 ScrollView
或一个 ListView
是不可能的,因为据我所知,它们不能像那样交叉放置两列。所以,我有两个解决方案。这些解决方法有不同的问题。
这是我的第一个解决方案:
-ScrollView 中的动态自定义视图。
- 这种方法的问题是我不知道如何更新 ScrollView
和动态自定义视图,所以它不会滚动。
我的第二种方案:
-两个独立连接的列表视图(如果你滑动一个,另一个会自动同步滑动到相同位置)。
-这个解决方案的问题是我找不到将它们相互连接的方法。
我们将不胜感激。
对于任何可能需要答案的人,我在 here. I improved this with that 中找到了解决方案(这是不正确的)(实际上我已将其修复)。这是我的最终解决方案:
MyScrollView.java
public class MyScrollView extends ScrollView implements IScrollListener {
private static final String TAG = "MyScrollView";
private IScrollListener listener;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setScrollViewListener(IScrollListener listener) {
this.listener = listener;
}
@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (listener != null) {
listener.onScrollChanged(this, x, y, oldX, oldY);
} else {
Log.i(TAG, "asdasd");
}
}
@Override
public void onScrollChanged(IScrollListener scrollView, int x, int y, int oldX, int oldY) {
super.onScrollChanged(x, y, oldX, oldY);
if (listener != null) {
listener.onScrollChanged(this, x, y, oldX, oldY);
} else {
Log.i(TAG, "asdasd");
}
}
}
MyActivity.java
public class MyActivity extends AppCompatActivity implements IScrollListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_position);
.
.
.
scrollViewLeft = (MyScrollView) this.findViewById(R.id.scrollViewLeft);
scrollViewRight = (MyScrollView) this.findViewById(R.id.scrollViewRight);
.
.
scrollViewLeft.setScrollViewListener(this);
scrollViewRight.setScrollViewListener(this);
.
.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLeft = vi.inflate(R.layout.target_item, null);
View viewRight = vi.inflate(R.layout.target_item_right, null);
// insert into left view
ViewGroup insertPointLeft = (ViewGroup) findViewById(R.id.leftLinearLayout);
insertPointLeft.addView(viewLeft, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// insert into right view
ViewGroup insertPointRight = (ViewGroup) findViewById(R.id.rightLinearLayout);
insertPointRight.addView(viewRight, targets.size(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// fill in any details dynamically here
TextView number = (TextView) viewLeft.findViewById(R.id.number);
number.setText("1");
TextView name = (TextView) viewLeft.findViewById(R.id.name);
name.setText("Muhammed Aydogan");
// fill in any details dynamically here
TextView number2 = (TextView) viewRight.findViewById(R.id.number);
number2.setText("2");
TextView name2 = (TextView) viewRight.findViewById(R.id.name);
name2.setText("Muhammed Aydogan");*/
}
@Override
public void onScrollChanged(IScrollListener scrollView, int x, int y, int oldX, int oldY) {
if (scrollViewLeft.equals(scrollView)) {
scrollViewRight.scrollTo(x, y);
} else if (scrollViewRight.equals(scrollView)) {
scrollViewLeft.scrollTo(x, y);
}
}
}
IScrollListener.java
public interface IScrollListener {
void onScrollChanged(IScrollListener scrollView, int x, int y, int oldX, int oldY);
}