我正在使用水平滚动视图来水平滑动屏幕,我想在当前视图上动态添加视图但它没有发生,我在代码中做错了什么.. 这是我的代码
public class MainActivity extends Activity implements OnGestureListener {
private GestureDetector gDetector;
private MyHorizontalScrollView scrollView;
private View app;
@TargetApi(11)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.sample, null));
//here I edited..
scrollView = (MyHorizontalScrollView) findViewById(R.id.myScrollView_);
LinearLayout layout = new LinearLayout(this);
scrollView.addView(layout);
ListView listView = new ListView(this);
initListView(this, listView, "Item ", 30, android.R.layout.simple_list_item_1);
layout.addView(listView);
//setContentView(R.layout.sample);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
getActionBar().hide();
}
findViewById(R.id.sample_button).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
SlideoutActivity.prepare(MainActivity.this, R.id.inner_content, width);
startActivity(new Intent(MainActivity.this,
MenuActivity.class));
overridePendingTransition(0, 0);
}
});
gDetector = new GestureDetector(this);
}
public static void initListView(Context context, ListView listView,String prefix, int numItems, int layout ){
// By using setAdpater method in listview we an add string array in list.
String[] arr = new String[numItems];
for(int i = 0; i< arr.length; i++){
arr[i] = prefix + (i +1);
}
listView.setAdapter(new ArrayAdapter<String>(context, layout, arr));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Context context = view.getContext();
String msg = "item[" + position + "]= " + parent.getItemIdAtPosition(position);
Toast.makeText(context, msg, 1000).show();
System.out.println(msg);
}
});
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity,
float yVelocity) {
if(start.getRawX() < finish.getRawX()){
/*((ViewGroup)scrollView.getParent()).removeView(scrollView);
LayoutInflater inflater = LayoutInflater.from(this);
scrollView = (MyHorizontalScrollView) findViewById(R.id.myScrollView_);
app = inflater.inflate(R.layout.horz_scroll_app, null);
ListView listView = (ListView) app.findViewById(R.id.list);
initListView(this, listView, "Item ", 30, android.R.layout.simple_list_item_1);
setContentView(listView);*/
TextView toolbartext = (TextView)findViewById(R.id.text);
toolbartext.setText("New text for samples from left to right");
Toast.makeText(getApplicationContext(), "from left to right Swipe working", Toast.LENGTH_LONG).show();
}else{
TextView toolbartext = (TextView)findViewById(R.id.text);
toolbartext.setText("New text for samples from right to left");
Toast.makeText(getApplicationContext(), "from right to left Swipe working", Toast.LENGTH_LONG).show();
}
return true;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gDetector.onTouchEvent(event);
}
}
她是我的logcat内容..
03-29 11:00:22.732: E/AndroidRuntime(303): FATAL EXCEPTION: main
03-29 11:00:22.732: E/AndroidRuntime(303): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-29 11:00:22.732: E/AndroidRuntime(303): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.view.ViewGroup.addView(ViewGroup.java:1865)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.view.ViewGroup.addView(ViewGroup.java:1845)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:217)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.app.Activity.setContentView(Activity.java:1658)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.example.sample.MainActivity.onFling(MainActivity.java:105)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:568)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.example.sample.MainActivity.onTouchEvent(MainActivity.java:146)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.app.Activity.dispatchTouchEvent(Activity.java:2089)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.os.Looper.loop(Looper.java:123)
03-29 11:00:22.732: E/AndroidRuntime(303): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 11:00:22.732: E/AndroidRuntime(303): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 11:00:22.732: E/AndroidRuntime(303): at java.lang.reflect.Method.invoke(Method.java:521)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 11:00:22.732: E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 11:00:22.732: E/AndroidRuntime(303): at dalvik.system.NativeStart.main(Native Method)