Home java Android Custom View (your VIEW on Android)

Android Custom View (your VIEW on Android)

Author

Date

Category

I have one overloaded layout for the list of chats. There is an autark, time, text, name / name, message status, and a bunch of smallest things, type icons online, chat icons, and message status.
I thought that it was very bad, and decided to draw my VIEW to optimize (the scrolling techniques will slow down on old devices). In general terms, it turns out, but there are some questions.

  1. CustomView in Baseadapter behaves like strange. When scrolling, the elements disappear, and when the scrolling stops, they appear, and redraw at times 10. How to fix it?

  2. In Canvas, there are some strange indents, for example, if you do not install canvas.translate, then the drawing is driving up and in the left. I put an indentation, and it seems fine, but will there be the most indents, sizes, and the like, on other screen extensions? (Transfer DP to PX, and I set them out as the size of the incidence, canvas, etc.)

  3. User avatars are loaded in another thread, after downloading them you need to show in my customview. How to “draw a” avatar after downloading? Redraw the whole view?

  4. Are there any similar guides, with examples, for drawing your View? In addition to official android.

The screen shows how the chats are displayed now, with my overloaded Layout

Forgive to explain everything is clear, preferably with examples, or links to similar things.

Thank you.


Answer 1, Authority 100%

Try on this example

it’s from the site

ListView Layout XML – This List – Container

& lt; relativeLayout XMLNS: Android = "http://schemas.android.com/apk/res/android"
  Android: layout_width = "fill_parent"
  Android: Layout_Height = "Match_Parent"
  Android: Orientation = "Vertical" & gt;
  & lt; ListView XMLNS: Android = "http://schemas.android.com/apk/res/android"
    Android: id = "@ + id / listview"
    Android: layout_width = "Match_Parent"
    Android: Layout_Height = "Match_Parent"
    Android: layout_marginbottom = "0dp"
    Android: layout_margintop = "0dp"
   / & gt;
& lt; / relativeLayout & gt;

This is the most important thing – here you change the element of the list according to your consideration

Custom Layout for Row in ListView

& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
& lt; RelativeLayout XMLNS: Android = "http://schemas.android.com/apk/res/android"
  Android: layout_width = "fill_parent"
  Android: layout_height = "fill_parent"
  Android: layout_marginbottom = "0dp"
  Android: layout_margintop = "0dp"
  Android: FocusableInTouchMode = "True" & gt;
  & lt; imageview
    Android: id = "@ + id / fruitimg"
    Android: layout_width = "wrap_content"
    Android: layout_height = "wrap_content"
    Android: Layout_Centervertical = "True"
    Android: paddingright = "10dp"
    Android: paddingtop = "10dp"
    Android: Paddingleft = "10dp"
    Android: scaletype = "centercrop" / & gt;
  & lt; TextView
    Android: id = "@ + ID / FruitName"
    Android: layout_width = "wrap_content"
    Android: layout_height = "wrap_content"
    Android: layout_torightof = "@ + id / fruitimg"
    Android: Layout_Centervertical = "True"
    Android: paddingtop = "10dp"
    Android: Text = "Cherry" / & gt;
  & lt; TextView
    Android: id = "@ + ID / Calories"
    Android: layout_width = "wrap_content"
    Android: layout_height = "wrap_content"
    Android: layout_alignparentright = "True"
    Android: Layout_Centervertical = "True"
    Android: paddingtop = "10dp"
    Android: paddingright = "10dp" / & gt;
& lt; / relativeLayout & gt;

ListAdapter – for processing

package com.javapapers.android.ListViewCustomLayout.app;
Import android.content.context;
Import Android.graphics.Bitmap; 
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class FruitArrayAdapter extends ArrayAdapter & lt; Fruit & gt; {
  private static final String TAG = "FruitArrayAdapter";
  private List & lt; Fruit & gt; fruitList = new ArrayList & lt; Fruit & gt; ();
  static class FruitViewHolder {
    ImageView fruitImg;
    TextView fruitName;
    TextView calories;
  }
  public FruitArrayAdapter (Context context, int textViewResourceId) {
    super (context, textViewResourceId);
  }
  @Override
  public void add (Fruit object) {
    fruitList.add (object);
    super.add (object);
  }
  @Override
  public int getCount () {
    return this.fruitList.size ();
  }
  @Override
  public Fruit getItem (int index) {
    return this.fruitList.get (index);
  }
  @Override
  public View getView (int position, View convertView, ViewGroup parent) {
    View row = convertView;
    FruitViewHolder viewHolder;
    if (row == null) {
      LayoutInflater inflater = (LayoutInflater) this.getContext (). GetSystemService (Context.LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate (R.layout.listview_row_layout, parent, false);
      viewHolder = new FruitViewHolder ();
      viewHolder.fruitImg = (ImageView) row.findViewById (R.id.fruitImg);
      viewHolder.fruitName = (TextView) row.findViewById (R.id.fruitName);
      viewHolder.calories = (TextView) row.findViewById (R.id.calories);
      row.setTag (viewHolder);
    } else {
      viewHolder = (FruitViewHolder) row.getTag ();
    }
    Fruit fruit = getItem (position);
    viewHolder.fruitImg.setImageResource (fruit.getFruitImg ());
    viewHolder.fruitName.setText (fruit.getFruitName ());
    viewHolder.calories.setText (fruit.getCalories ());
    return row;
  }
  public Bitmap decodeToBitmap (byte [] decodedByte) {
    return BitmapFactory.decodeByteArray (decodedByte, 0, decodedByte.length);
  }
}

ListView Activity – with examples

package com.javapapers.android.listviewcustomlayout.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ListViewActivity extends Activity {
  private static final String TAG = "ListViewActivity";
  private FruitArrayAdapter fruitArrayAdapter;
  private ListView listView;
  private static int colorIndex;
  @Override
  public void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.listview_layout);
    colorIndex = 0;
    listView = (ListView) findViewById (R.id.listView);
    fruitArrayAdapter = new FruitArrayAdapter (getApplicationContext (), R.layout.listview_row_layout);
    listView.setAdapter (fruitArrayAdapter);
    List & lt; String [] & gt; fruitList = readData ();
    for (String [] fruitData: fruitList) {
      String fruitImg = fruitData [0];
      String fruitName = fruitData [1];
      String calories = fruitData [2];
      int fruitImgResId = getResources (). getIdentifier (fruitImg, "drawable", "com.javapapers.android.listviewcustomlayout.app");
      Fruit fruit = new Fruit (fruitImgResId, fruitName, calories);
      fruitArrayAdapter.add (fruit);
    }
  }
  public List & lt; String [] & gt; readData () {
    List & lt; String [] & gt; resultList = new ArrayList & lt; String [] & gt; ();
    String [] fruit7 = new String [3];
    fruit7 [0] = "orange";
    fruit7 [1] = "Orange";
    fruit7 [2] = "47 Calories";
    resultList.add (fruit7);
    String [] fruit1 = new String [3];
    fruit1 [0] = "cherry";
    fruit1 [1] = "Cherry";
    fruit1 [2] = "50 Calories";
    resultList.add (fruit1);
    String [] fruit3 = new String [3];
    fruit3 [0] = "banana"; 
Fruit3 [1] = "Banana";
    Fruit3 [2] = "89 Calories";
    resultlist.add (Fruit3);
    String [] Fruit4 = New String [3];
    Fruit4 [0] = "Apple";
    Fruit4 [1] = "Apple";
    Fruit4 [2] = "52 Calories";
    resultlist.add (Fruit4);
    String [] Fruit10 = New String [3];
    Fruit10 [0] = "kiwi";
    Fruit10 [1] = "kiwi";
    Fruit10 [2] = "61 Calories";
    resultlist.add (Fruit10);
    String [] Fruit5 = New String [3];
    Fruit5 [0] = "PEAR";
    Fruit5 [1] = "Pear";
    Fruit5 [2] = "57 Calories";
    resultlist.add (fruit5);
    String [] Fruit2 = New String [3];
    Fruit2 [0] = "Strawberry";
    Fruit2 [1] = "Strawberry";
    Fruit2 [2] = "33 Calories";
    resultlist.add (Fruit2);
    String [] Fruit6 = New String [3];
    Fruit6 [0] = "Lemon";
    Fruit6 [1] = "Lemon";
    Fruit6 [2] = "29 Calories";
    resultlist.add (Fruit6);
    String [] Fruit8 = New String [3];
    Fruit8 [0] = "peach";
    Fruit8 [1] = "peach";
    Fruit8 [2] = "39 Calories";
    resultlist.add (Fruit8);
    String [] Fruit9 = New String [3];
    Fruit9 [0] = "Apricot";
    Fruit9 [1] = "Aprot";
    Fruit9 [2] = "48 Calories";
    resultlist.add (Fruit9);
    String [] Fruit11 = New String [3];
    Fruit11 [0] = "Mango";
    Fruit11 [1] = "Mango";
    Fruit11 [2] = "60 Calories";
    resultlist.add (Fruit11);
    Return ResultList;
  }
}

Answer 2

In fact, it is enough to use RecyclerView and optimally contemplate this list of list. Under the “optimal” I mean to exclude nesting and let’s say everything with RelativeLayout.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions