attrs.xml
<resources>
<declare-styleable name="WorkStationListView">
<attr name="type" format="string" />
</declare-styleable>
</resources>
<com.mingshine.cloud.view.WorkStationListView
android:id="@+id/lv_work_station_row"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:listSelector="@drawable/selector_work_station"
app:type="ROW" />
WorkStationListView.java
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.hikari.hikaricloud.R;
public class WorkStationListView extends ListView {
public String[] dataList;
private String type;
public WorkStationListView(Context context) {
this(context, null);
}
public WorkStationListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WorkStationListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
handleCustomAttrs(context, attrs);
initData();
initAdapter(context);
}
private void initAdapter(Context context) {
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(context, R.layout.item_work_station, dataList);
setAdapter(arrayAdapter);
}
private void initData() {
if (type.equals("ROW")) {
dataList = new String[100];
for (int i = 0; i < 100; i++) {
dataList[i] = (i + 1) + "组";
}
} else {
dataList = new String[100];
for (int i = 0; i < 100; i++) {
dataList[i] = (i + 1) + "列";
}
}
}
private void handleCustomAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WorkStationListView);
type = typedArray.getString(R.styleable.WorkStationListView_type);
}
}