Я отчаянно нуждаюсь в помощи. Я пытался решить свои проблемы в течение нескольких дней, и я теряю желание продолжать свой проект. Кажется, я просто не понимаю, поэтому, пожалуйста, не могли бы вы помочь мне сделать это.
Я пытаюсь создать Activity_all_friends. xml, который содержит список друзей. Моя идея заключалась в том, что у меня будет линейный макет, который содержит пользовательские представления ("friendView").
Я создал FriendView:
![enter image description here](https://i.stack.imgur.com/HEbin.png)
<?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"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/friendImage"
android:layout_width="100dp"
android:layout_height="match_parent"
android:contentDescription="Person Image"
app:srcCompat="@drawable/person2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/friendName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Name of friend"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionInPersonIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/in_person" />
<TextView
android:id="@+id/interactionInPersonText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionVideoIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:tint="#292828"
app:srcCompat="@android:drawable/presence_video_online" />
<TextView
android:id="@+id/interactionVideoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionTextIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:srcCompat="@drawable/text_icon" />
<TextView
android:id="@+id/interactionTextText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/interactionPhoneIcon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:tint="#353535"
app:srcCompat="@drawable/phone_icon" />
<TextView
android:id="@+id/interactionPhoneText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="299"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
В любом случае, когда я пытаюсь нарисовать этот новый вид на моем макете activity_all_friends , это выглядит пустым:
![enter image description here](https://i.stack.imgur.com/GLlAO.png)
Я также хочу добавить этих новых друзей, нажав кнопку «Создать новый» в правом верхнем углу. Я назначил setOnClickListener для кнопки. Нажатие кнопки работает, и новые элементы кажутся отключенными, я просто не вижу их.
Мой класс FriendView:
public class FriendView extends LinearLayout {
private Context context;
private String friend;
public FriendView(Context context) {
super(context);
init(null,0);
}
public FriendView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs,0);
}
public FriendView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs,defStyle);
init(attrs,0);
}
private void init(AttributeSet attrs, int defStyle){
}
public String getFriend() {
return friend;
}
public void setFriend(String friend) {
this.friend = friend;
}
public View getView(View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.friend_view, null);
TextView friendName = (TextView) view.findViewById(R.id.friendView);
TextView interactionInPersonText = (TextView) view.findViewById(R.id.interactionInPersonText);
TextView interactionVideoText = (TextView) view.findViewById(R.id.interactionVideoText);
TextView interactionTextText = (TextView) view.findViewById(R.id.interactionTextText);
TextView interactionPhoneText = (TextView) view.findViewById(R.id.interactionPhoneText);
// TODO
friendName.setText("Temp random");
interactionInPersonText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionVideoText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionTextText.setText(Long.toString(Math.round(Math.random() * 300)));
interactionPhoneText.setText(Long.toString(Math.round(Math.random() * 300)));
return view;
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawColor(Color.BLUE);
}
}
Кто-нибудь получил какие-либо идеи относительно того, почему это происходит? Насколько я понимаю, мне нужно написать элемент xml, который я хочу сгенерировать (friend_view. xml в моем случае), я хочу написать класс, который динамически обрабатывает то, что происходит с элементом (класс friendView в моем случае), а затем я хочу связать это с макетом, где я хочу, чтобы он был сгенерирован (activity_all_friends. xml в моем случае). Это правильно?
2 других маленьких вопроса: у меня есть friends_view. xml в папке макета, это правильно? Откуда приходят атрибуты (attr. xml)? Я хочу использовать мой API и динамически менять номера и имена друзей.
Спасибо за вашу помощь !!