我有 2 个 ListView,它们是自动滚动的。因此,当从那些 listViews 中选择 2 个项目时,将打开一个新窗口(Canvas),但我希望该新窗口(Canvas)可滚动。因为画布太大了……
需要帮忙!提前致谢..
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ListView
android:id="@+id/list1"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" />
<ListView
android:id="@+id/list2"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" />
<View
android:id="@+id/mapView"
android:layout_width="60dp"
android:layout_height="fill_parent"/>
</RelativeLayout>
- 当我从列表视图中选择 2 个项目时,我希望(视图)可以在新窗口中滚动。
活动源代码
package com.example.tester1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
Map map;
String Source = "Blank";
String Destination = "Blank";
ListView list1;
ListView list2;
String [] listArray;
BufferedReader AirportLocation;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
AirportLocation = new BufferedReader(
new InputStreamReader(getAssets().open("Airport-Location3.txt")));
} catch (IOException e) {
e.printStackTrace();
}
listArray = new String[1168];
String line1;
int c = 0;// location detector
try {
while((line1 = AirportLocation.readLine())!=null){
StringTokenizer s = new StringTokenizer(line1);
String airport = s.nextToken();
listArray[c] = airport;
c++;
}
} catch (IOException e) {
e.printStackTrace();
}
list1 = (ListView) findViewById(R.id.list1);
list2 = (ListView) findViewById(R.id.list2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listArray);
list1.setAdapter(adapter1);
list2.setAdapter(adapter2);
list1.setOnItemClickListener(new SourceSelection());
list2.setOnItemClickListener(new DestinationSelection());
}
public void Draw(){
map = new Map(this);
setContentView(map);
}
class SourceSelection implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView temp = (TextView) arg1;
Source = temp.getText()+"";
if(!Source.equals("Blank") && !Destination.equals("Blank")){
Draw();
}
}
}
class DestinationSelection implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView temp = (TextView) arg1;
Destination = temp.getText()+"";
if(!Source.equals("Blank") && !Destination.equals("Blank")){
Draw();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Map extends View {
Paint paint = new Paint();
public Map(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawCircle(100, 100, 20, paint);
canvas.drawLine(2000, 2000, 5000, 5000, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Compute the height required to render the view
// Assume Width will always be MATCH_PARENT.
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
setMeasuredDimension(width, height);
}
}
}