我正在绘制矩形视图并将其排列在HorizontalScrollView中。我想要一个功能,当单击一个矩形(视图)时,它会增大,但其他矩形(视图)大小应该保持不变。
我有一个类LockHorizontalScrollview,它通过调用CustomListAdapter类的getView()方法绘制初始视图。另外,我在getView()方法中附加onClicklistener。
因此,当点击任何视图时,我正在重绘HorizontalScrollView的所有视图。但我面临一个问题,如何告诉LockHorizontalScrollview(这个类将绘制我的所有视图)类再次重绘所有视图。
我遇到的问题是当我点击任何视图时,应用程序崩溃
LockHorizontalScrollview类由main活动调用。此类调用自定义Adapter的getView()方法来绘制所有视图。该类绘制所有视图。
public class LockHorizontalScrollview extends HorizontalScrollView {
Context context;
int prevIndex = 0;
int data[] = new int[] {200,200,200,200,200,200,200,200,200,200,200,200};
CustomListAdapter kk;
void changedata(int position)
{
data[position]+=50;
abc(kk);
}
public void setAdapter(Context context, CustomListAdapter mAdapter) {
try {
this.kk=mAdapter;
fillViewWithAdapter(mAdapter);
} catch (ZeroChildException e) {
e.printStackTrace();
}
}
private void fillViewWithAdapter(CustomListAdapter mAdapter)
throws ZeroChildException {
if (getChildCount() == 0) {
throw new ZeroChildException(
"CenterLockHorizontalScrollView must have one child");
}
if (getChildCount() == 0 || mAdapter == null)
{ Log.e("Ronak","Came at zero");
return;
}
ViewGroup parent = (ViewGroup) getChildAt(0);
//parent.removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
parent.addView(mAdapter.getView(i, null, parent));
}
}
void abc(CustomListAdapter mAdapter)
{
Log.e("Ronak","Came at");
ViewGroup parent = (ViewGroup) getChildAt(0);
//parent.removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
parent.addView(mAdapter.getViewType(i, null, parent,data[i]));
}
}
}
我的自定义适配器
public class CustomListAdapter extends BaseAdapter {
static class ViewHolder {
public DrawView drawview;
public TextView textview;
public LinearLayout ll;
}
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
Context c;
HorizontalScrollViewActivity abc=new HorizontalScrollViewActivity();
//CenterLockHorizontalScrollview abc=new CenterLockHorizontalScrollview(c, null);
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public CustomListAdapter(Context c, ArrayList<String> data) {
mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData=data;
this.c=c;
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private int getWidth(String aa)
{
int k=aa.length();
if(k>0 && k<=3)
return 200;
if(k>3 && k<=6)
return 250;
if(k>6 && k<=9)
return 300;
if(k>9 && k<=12)
return 350;
if(k>12 && k<=15)
return 400;
if(k>15 && k<=18)
return 450;
if(k>18 && k<=21)
return 500;
if(k>21 && k<=24)
return 550;
if(k>24 && k<=27)
return 600;
return 700;
}
private int getHeight(String aa)
{
return 350;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
Log.e("Ronak","Came at getview");
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.news_list_item,null);
final LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.mainLayout);
//final LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(300,400);
//layout.setLayoutParams(parms);
TextView t= (TextView)convertView.findViewById(R.id.textView1);
//t.setWidth(width);
//t.setBackgroundColor(0x7F2626);
holder.textview = t;
holder.ll=layout;
DrawView abc = (DrawView)convertView.findViewById(R.id.drawview);
holder.drawview=abc;
Log.e("Ronak","reached here3");
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textview.setText(mData.get(position));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c, "Clicked on="+position, 2).show();
// Log.e
abc.get().changedata(position);
}
});
return convertView;
}
public View getViewType(final int position, View convertView, final ViewGroup parent, int width) {
convertView = mInflater.inflate(R.layout.news_list_item,null);
final LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.mainLayout);
final LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,150);
layout.setLayoutParams(parms);
return convertView;
}
还有绘制矩形的DrawView.java类
和我的xml news_list_item.xml,它是水平滚动视图的每个视图的布局
我的主要活动是
public class HorizontalScrollViewActivity extends Activity {
CenterLockHorizontalScrollview centerLockHorizontalScrollview;
CustomListAdapter customListAdapter;
Button btnPrev, btnNext, btnActivity;
int currIndex = 0;
private TextView text;
int data[] = new int[] {200,200,200,200,200,200,200,200,200,200,200,200};
ArrayList<String> list = new ArrayList<String>() {
{
add("Jan");
add("Feb");
add("Mar");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e("Ronak","Came at main");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_scroll_view);
centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) findViewById(R.id.scrollView);
customListAdapter = new CustomListAdapter(this, list);
centerLockHorizontalScrollview.setAdapter(this, customListAdapter);
}
答案 0 :(得分:1)
您正在寻找的方法是requestLayout()
。调用它将使被调用者重新测量并重新布局所有孩子。
在旁注中,您使用Adapter
HorizontalScrollView
(不是AdapterView
)是否有任何特殊原因?
使用Adapter/AdapterView
背后的整个想法是查看回收 - ScrollView
s不这样做。由于您不使用ListView
(或其他AdapterView
),因此使用适配器会过度。
另外,我认为将内容填充逻辑放入ScrollView
也不是一个好主意。您可能应该在它的子代(代码中名为parent
的代码)中执行此操作。 ScrollViews
旨在提供将滚动功能添加到任何其他视图的简单方法 - 只需将其包装在布局文件中;用内容感知逻辑扩展它们是违背他们的追求:)