public class MainActivity extends AppCompatActivity {
private Unbinder unbinder;
@BindView(R.id.gv_id)
GridView gv;
private List<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
unbinder = ButterKnife.bind(this);
for(int i = 0;i<30;i++)
{
data.add("Item -- " + i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_gv,R.id.tv,data);
gv.setAdapter(adapter);
//设置监听器
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO --------显示服务器获取的数据
//todo 收到数据后, 显示对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View dv = getLayoutInflater().inflate(R.layout.dv,null);
builder.setView(dv);
builder.show();
}
});
}
}
public class CustomView extends View {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(300,300,300,paint);
canvas.drawCircle(300,300,200,paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(50);
canvas.drawText("12345",250,300,paint);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="bw.com.test.MainActivity">
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:id="@+id/gv_id" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:id="@+id/tv"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="300dp">
<bw.com.test.CustomView
android:layout_width="300dp"
android:layout_height="300dp" />
</LinearLayout>