美好的一天。
我需要什么: 我需要创建一个带有自定义行的ListView,当用户滚动这些行时,它会将信息异步加载到行中。 喜欢 -
问题: 任何人都可以描述一种模式,以及我应该用来实现这一目标的元素吗?
尝试:
我得到了什么:
每次渲染ListView时都会拍摄此事件,并且从一开始就为每个项目拍摄...
任何建议都将不胜感激,谢谢。
一些代码
活动,onResume事件。
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
tvl = (TextView) findViewById(R.id.tvLog);
LinearLayout lv = (LinearLayout) findViewById(R.id.Ll1);
LinkedList<String> ll = new LinkedList<>();
for (int i = 0; i < 30; i++) {
ll.add("s"+i);
}
CommonCheckRowAdapter cma = new CommonCheckRowAdapter(getApplication(), ll);
int adapterCount = cma.getCount();
for (int i = 0; i < adapterCount; i++) {
View item = cma.getView(i, null, null);
lv.addView(item);
}
}
自定义视图类主体
package com.example.testscroll;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnDrawListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
public class CView extends FrameLayout{
private LayoutInflater inflater;
public CView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setWillNotDraw(false);
}
public CView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotDraw(false);
}
public CView(Context context) {
super(context);
this.setWillNotDraw(false);
}
private void initView(Context context){
View.inflate(context, R.layout.common_row, this);
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
//Do what you want.
//Toast.makeText(getContext(), "Showing.", Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "Showing " + ((TextView)findViewById(R.id.tv1)).getText().toString(), Toast.LENGTH_SHORT).show();
MainActivity.tvl.setText(MainActivity.tvl.getText().toString() + " "+ ((TextView)findViewById(R.id.tv1)).getText().toString());
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
}
适配器的getView(休息 - 在书中)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.common_row, null);
//Fill
((TextView)vi.findViewById(R.id.tv1)).setText(data.get(position));
return vi;
}
和commoon_row布局
<?xml version="1.0" encoding="utf-8"?>
<com.example.testscroll.CView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_margin="10dp"
android:gravity="center_horizontal|center_vertical"
android:minLines="2"
android:text="www"
android:textSize="50sp" >
</TextView>
</com.example.testscroll.CView>
答案 0 :(得分:0)
private String text = "";
public CView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setWillNotDraw(false);
}
public CView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotDraw(false);
}
public CView(Context context) {
super(context);
this.setWillNotDraw(false);
}
private void initView(Context context){
View.inflate(context, R.layout.common_row, this);
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
//Do what you want.
//Toast.makeText(getContext(), "Showing.", Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "Showing " + ((TextView)findViewById(R.id.tv1)).getText().toString(), Toast.LENGTH_SHORT).show();
MainActivity.tvl.setText(text);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
public void update(){
text = MainActivity.tvl.getText().toString() + " "+ ((TextView)findViewById(R.id.tv1)).getText().toString();
}
<强>适配器:强>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.common_row, null);
//Fill
((TextView)vi.findViewById(R.id.tv1)).setText(data.get(position));
((vi)vi).update();
return vi;
}