Я разрабатываю карточную игру, в которой я должен отображать счет каждого игрока после каждого раунда.Для этого я создал класс табло и использовал Listview для отображения имени и счета игрока.
Ниже приведен мой код
Класс ScoreCardPopup
открытый класс ScoreCardPopup {
private List<Items> itemsList = new ArrayList<Items>();
private ListView listView;
private CustomListAdapter adapter;
private ArrayList<String> playernames = new ArrayList<>();
private ArrayList<Integer> playerscore = new ArrayList<>();
Context context;
public ScoreCardPopup(ArrayList<String> playernames, ArrayList<Integer> playerscore) {
this.playernames = playernames;
this.playerscore = playerscore;
}
public ScoreCardPopup(ArrayList<String> playernames, ArrayList<Integer> playerscore, Context context) {
this.playernames = playernames;
this.playerscore = playerscore;
this.context = context;
}
public void showScoreCard() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_score_card);
//Initialize and create a new adapter with layout named list found in activity_main layout
listView = (ListView) dialog.findViewById(R.id.list);
adapter = new CustomListAdapter(context, itemsList);
listView.setAdapter(adapter);
int i = 0;
while (i < playernames.size()) //Add to the Items array
{
Items items = new Items();
items.setName(playernames.get(i).toString());
items.setScore(playerscore.get(i).toString());
itemsList.add(items);
i++;
}
//All done, so notify the adapter to populate the list using the Items Array
adapter.notifyDataSetChanged();
dialog.show();
}
}
Класс CustomListAdapter
public class CustomListAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<Items> items;
public CustomListAdapter(Context context, List<Items> items) {
this.context = context;
this.items = items;
}
/**
* How many items are in the data set represented by this Adapter.
*
* @return Count of items.
*/
@Override
public int getCount() {
return items.size();
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want within the adapter's
* data set.
* @return The data at the specified position.
*/
@Override
public Object getItem(int position) {
return items.get(position);
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Get a View that displays the data at the specified position in the data set. You can either
* create a View manually or inflate it from an XML layout file. When the View is inflated, the
* parent View (GridView, ListView...) will apply default layout parameters unless you use
* {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param scoreView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return A View corresponding to the data at the specified position.
*/
@Override
public View getView(int position, View scoreView, ViewGroup parent) {
ViewHolder holder;
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (scoreView == null) {
scoreView = inflater.inflate(R.layout.list_row, parent, false);
holder = new ViewHolder();
holder.name = (TextView) scoreView.findViewById(R.id.name);
holder.score = (TextView) scoreView.findViewById(R.id.score);
scoreView.setTag(holder);
} else {
holder = (ViewHolder) scoreView.getTag();
}
final Items m = items.get(position);
holder.name.setText(m.getName());
holder.score.setText(m.getScore());
return scoreView;
}
static class ViewHolder {
TextView name;
TextView score;
}
}
Элементы класса
public class Items {
private String name, score;
public Items() {
}
public Items(String name, String score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="16dp"
android:textColor="#222222" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:textSize="14dp"
android:textColor="#222222" />
</RelativeLayout>
score_card.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ListView 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:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:cacheColorHint="#FFFFFF"
android:clipToPadding="false"
android:dividerHeight="1dp"
android:paddingBottom="5dp"
tools:context=".ScoreCard.ScoreCard"></ListView>
<Button
android:id="@+id/OK"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="134dp"
android:layout_marginTop="502dp"
android:paddingBottom="0dp"
android:text="@string/button_OK" />
</RelativeLayout>
После реализации этого кода я смог создать горизонтальный список игрокови соответствующий им общий балл.
Изображение табло.
Но теперь я хочу отобразить его вертикально, вместе со счетомкаждый раунд, так как мне нужно отображать рейтинг каждого игрока после каждого раунда.Как показано на рисунке ниже
Любые предложения относительно внедрения новой системы показателей будут действительно полезны.