14

Ich arbeite an einem Android-Projekt, in dem ich das 'SuperSlim' Framework zum Erstellen einer Rasteransicht von Notes (benutzerdefinierte Klasse) zusammen mit Daten angezeigt werden soll . Hinweise in der Datenbank haben eine Viele-zu-eins-Beziehung mit Abschnitten (Benutzerdefinierte Klasse). Und Abschnitte wiederum haben eine Beziehung von mehreren zu Canvas.Android: Putting Grid von dynamischen und benutzerdefinierten Objekten in einem anderen Raster von dynamischen und benutzerdefinierten Objekten

Alle Informationen für Abschnitt, Notizen wird dynamisch vom Server als Liste abgerufen.

Jetzt bin ich an dem Punkt, wo ich in der Lage bin, eine Rasteransicht von Sektionen anzuzeigen, und Textinformation im Grid wie Abschnittsname, etc. zu Testzwecken, fügte ich auch statisch abgerufenen Text aus eine Notiz. Ich bin neu in der Android-Programmierung, also bitte nicht stören, wenn der Code durcheinander sieht.

Nun sind dies die Probleme, die ich bin vor:

1) Wie ein Raster der Abschnitte angezeigt werden und in jedem Abschnitt angezeigt, würde Ich mag ein Gitter von Notizen angezeigt werden soll. Da es eine Eins-zu-viele-Beziehung gibt, kann es viele Hinweise pro Abschnitt geben. Das ist mein Hauptproblem.

2) Bei der Anzeige der oben genannten Sachen möchte ich das SectionName-Feld editierbar lassen, und ich habe eine REST-Methode, mit der ich den Abschnittsname bearbeiten kann, aber ich benötige auch die Abschnitts-ID. Es ist auf Klick verfügbar, das möchte ich beibehalten.

3) Das Gitternetz von Notizen, das in einem Abschnitt angezeigt wird, sollte anklickbar sein, damit ich später etwas ähnliches wie Modal öffnen kann, so dass Benutzer die gesamte Notiz lesen und bearbeiten können.

Der Screenshot unten zeigt meine aktuelle Situation:

Screenshot Die linke Seite Handy war die ursprüngliche, wie Liste der Gitterabschnitte aussehen sollte. Ich änderte es, um mehr Informationen anzuzeigen, nur um zu testen, und SuperSlim zu verwenden, um Informationen etwas genauer hinzuzufügen.

Bitte beachten Sie, derzeit im Code, ich rufe statisch die NotesList-Methode für eine hardcoded Abschnitt. Dies ist nicht erwünscht. Schließlich Code:

GroupSectionActivity:

public class GroupSectionActivity extends ActionBarActivity { 

    private SectionServiceImpl sectionService = new SectionServiceImpl(); 

    private static volatile List<RestSection> restSectionList = new ArrayList<>(); 

    private static volatile Long groupAccountId; 

    private static volatile Integer canvasid; 

    static final String msectionname = "msectionname"; 
    static final String msectionid = "msectionid"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_section_activity); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      groupAccountId = extras.getLong("groupid"); 
      canvasid = extras.getInt("canvasid"); 
     } 

     if(savedInstanceState == null){ 
      getFragmentManager().beginTransaction().add(R.id.container, new NoteFragments(), "msectionname").commit(); 
     } 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if(toolbar!=null){ 
      setSupportActionBar(toolbar); 
     } 

     restSectionList = this.sectionService.getSectionByCanvas(canvasid); 

     ArrayList<HashMap<String, String>> restSectionArrayList = new ArrayList<HashMap<String, String>>(); 
     for (RestSection restSection : restSectionList) { 

      HashMap<String, String> sectionDisplay = new HashMap<>(); 
      sectionDisplay.put("msectionid", String.valueOf(restSection.getMsectionid())); 
      sectionDisplay.put("msectionname", restSection.getMsectionname()); 
      restSectionArrayList.add(sectionDisplay); 
     } 

     /* listView = (ListView) findViewById(R.id.seclist); 

     sectionLazyAdapter = new SectionLazyAdapter(this, restSectionArrayList); 

     listView.setAdapter(sectionLazyAdapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       int sectionId = restSectionList.get(position).getMsectionid(); 
       Log.d("Sectionid is ", String.valueOf(sectionId)); 
       *//*Intent intent = new Intent(GroupSectionActivity.this, GroupSectionActivity.class); 
       intent.putExtra("groupid", groupAccountId); 
       intent.putExtra("sectionid", sectionId); 
       startActivity(intent); 
       finish();*//* 

      } 
     }); 

     addSectionButton = (Button) findViewById(R.id.sectionAddButton); 
     addSectionButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       int val = addGroupSection(); 
       if (val == 0) { 
        sectionName.setError("Section Name cannot be null"); 
       } else { 
        sectionName.clearComposingText(); 
        sectionName.clearAnimation(); 
        sectionName.setText(""); 
        Toast.makeText(getApplicationContext(), "Section added", Toast.LENGTH_LONG).show(); 
       } 
      } 
     });*/ 

    } 

    public Integer addGroupSection(){ 
    /* sectionName = (EditText) findViewById(R.id.sectionNameTextField); 
     if (!(sectionName.getText().toString().isEmpty())) { 
      RestSection restSection = new RestSection(); 
      restSection.setMsectionname(sectionName.getText().toString()); 
      return this.sectionService.addGroupSection(restSection,canvasid); 
     } 
*/ 
     return 0; 
    } 

    @Override 
    public void onBackPressed() { 
     Intent intent = new Intent(GroupSectionActivity.this, GroupCanvasActivity.class); 
     intent.putExtra("groupid", groupAccountId); 
     startActivity(intent); 
     finish(); 
    } 

    private NoteFragments getSectionsFragment() { 
     return (NoteFragments) getFragmentManager().findFragmentByTag(msectionname); 
    } 
} 

SectionLazyAdapter:

public class SectionLazyAdapter extends BaseAdapter{ 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 

    public SectionLazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.activity_group_section, null); 

     TextView sectionName = (TextView)vi.findViewById(R.id.sectionname); // title 
     HashMap<String, String> sectionList = new HashMap<String, String>(); 
     sectionList = data.get(position); 

     sectionName.setText(sectionList.get(GroupSectionActivity.msectionname)); 

     return vi; 
    } 
} 

NoteAdapters:

public class NoteAdapters extends RecyclerView.Adapter<NoteViewHolder> { 

    private NoteServiceImpl noteService = new NoteServiceImpl(); 

    private static final int LINEAR = 0; 

    private final Context mContext; 

    private SectionServiceImpl sectionService = new SectionServiceImpl(); 

    List<RestSection> restSectionList = new ArrayList<>(); 


    private final ArrayList<LineItem> mItems; 

    public NoteAdapters(Context context, int headermode) { 
     mContext = context; 

     int sectionManager = -1; 

     int sectionFirstPosition = 0; 

     mItems = new ArrayList<>(); 

     restSectionList = this.sectionService.getSectionByCanvas(2500); 

     for (int i = 0; i < restSectionList.size(); i++) { 
      String header = restSectionList.get(i).getMsectionname(); 
      RestNote restNote = this.noteService.getFirstNoteForSection(restSectionList.get(i).getMsectionid()); 
      mItems.add(new LineItem(header, true, sectionManager, sectionFirstPosition, restNote.getMnotetext())); 

     } 
    } 

    @Override 
    public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view; 
     view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_group_section, parent, false); 
     return new NoteViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(NoteViewHolder holder, int position) { 
     final LineItem item = mItems.get(position); 
     final View itemView = holder.itemView; 
     holder.bindText(item.text); 
     holder.bindNoteData(item.otherText); 
     final GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(itemView.getLayoutParams()); 

     lp.setSlm(item.sectionManager == LINEAR ? LinearSLM.ID : GridSLM.ID); 
     lp.setColumnWidth(mContext.getResources().getDimensionPixelSize(R.dimen.grid_column_width)); 
     lp.setFirstPosition(item.sectionFirstPosition); 
     itemView.setLayoutParams(lp); 
    } 

    @Override 
    public int getItemCount() { 
     return mItems.size(); 
    } 

    /* @Override 
    public void onClick(View v) { 
     if(v instanceof ImageView){ 
      Log.d("Image","Clicked"); 
     } else { 
      Log.d("Text","Clicked"); 
     } 
    }*/ 

    private static class LineItem { 

     public int sectionManager; 

     public int sectionFirstPosition; 

     public boolean isHeader; 

     public String text; 

     public String otherText; 

     public LineItem(String text, boolean isHeader, int sectionManager, 
         int sectionFirstPosition, String otherText) { 
      this.isHeader = isHeader; 
      this.text = text; 
      this.sectionManager = sectionManager; 
      this.sectionFirstPosition = sectionFirstPosition; 
      this.otherText = otherText; 
     } 
    } 
} 

NoteFragments:

public class NoteFragments extends Fragment { 

    private ViewHolder mViews; 

    private NoteAdapters noteAdapters; 

    private int mHeaderDisplay; 

    private boolean mAreMarginsFixed; 

    private Random mRng = new Random(); 

    private Toast mToast = null; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.section_fragment, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     mViews = new ViewHolder(view); 
     mViews.initViews(new LayoutManager(getActivity())); 
     noteAdapters = new NoteAdapters(getActivity(), mHeaderDisplay); 
     mViews.setAdapter(noteAdapters); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState){ 
     super.onSaveInstanceState(outState); 
    } 

    private static class ViewHolder { 

     private final RecyclerView mRecyclerView; 

     public ViewHolder(View view) { 
      mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); 
     } 

     public void initViews(LayoutManager lm) { 
      mRecyclerView.setLayoutManager(lm); 
     } 

     public void scrollToPosition(int position) { 
      mRecyclerView.scrollToPosition(position); 
     } 

     public void setAdapter(RecyclerView.Adapter<?> adapter) { 
      mRecyclerView.setAdapter(adapter); 
     } 

     public void smoothScrollToPosition(int position) { 
      mRecyclerView.smoothScrollToPosition(position); 
     } 
    } 

} 

NoteViewHolder:

public class NoteViewHolder extends RecyclerView.ViewHolder { 

    private TextView textView; 
    private TextView noteData; 
    private ImageView imageView; 

    public NoteViewHolder(View itemView) { 
     super(itemView); 
     textView = (TextView) itemView.findViewById(R.id.sectionname); 
     imageView = (ImageView) itemView.findViewById(R.id.sectionimage); 
     noteData = (TextView) itemView.findViewById(R.id.noteText); 
    } 

    public void bindText(String text){ 
     textView.setText(text); 
    } 

    public void bindImage(Bitmap bitmap){ 
     imageView.setImageBitmap(bitmap); 
    } 

    public void bindNoteData(String data){ 
     noteData.setText(data); 
    } 
} 

XML-Dateien: activity_group_section.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:orientation="horizontal" 
    android:padding="5dip" > 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/sectionimage" 
      android:layout_width="140dp" 
      android:layout_height="200dp" 
      android:scaleType="fitXY" 
      android:padding="5dp" 
      android:src="@drawable/sectionbackground" 
      /> 

     <TextView 
      android:id="@+id/sectionname" 
      android:layout_width="90dp" 
      android:layout_height="match_parent" 
      android:text="@string/textView" 
      android:visibility="visible" 
      android:gravity="center" 
      android:layout_gravity="center_horizontal|top" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:scrollHorizontally="true" 
      android:layout_marginTop="10dp" /> 

     <TextView 
      android:layout_width="97dp" 
      android:layout_height="160dp" 
      android:id="@+id/noteText" 
      android:layout_gravity="center_horizontal|bottom" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="30dp" /> 
    </FrameLayout> 
</RelativeLayout> 

SectionFragment:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/recycler_view" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:clipToPadding="false" 
    android:layout_height="wrap_content" /> 

Modellklassen für Abschnitt und Anmerkungen:

public class RestSection { 

    private int msectionid; 

    private String msectionname; 

    private int mxposition; 

    private int myposition; 

    private int msectionwidth; 

    private int msectionheight; 
} 
public class RestNote { 

    private int mnoticesid; 

    private String mnotetext; 

    private String mnotetag; 

    private String mnotecolor; 

    private double mnoteorder; 
} 

ich meine Frage hoffen klar ist, ob es etwas erforderlich ist, lassen Sie mich freundlich wissen.

Antwort

1

Sie sind eigentlich ziemlich nah an Ihrer Implementierung, aber es gibt ein paar Dinge zu beachten. Ihr Top-Level-Grid ist für die Verwendung von ListView/GridView-Adaptern eingerichtet, während Ihr Second-Level-Grid für RecyclerView.Adapters eingerichtet ist. All das ist gut und gut, wenn Sie wissen, was Sie tun. Ich würde jedoch empfehlen, den einen oder anderen durch den ganzen Weg zu verwenden (vorzugsweise RecyclerView.Adapters, da diese besser skalierbar sind). Um die Dinge einfach zu halten, obwohl, werde ich die Lösung mit Ihrem aktuellen Setup geben:

Top-Level (BaseAdapter/ListAdapter + Gridview)

Sie sollten in Ihrer gesamten werden Gabe von Daten-Set auf der obersten Ebene Adapter, z ArrayList von RestSection anstelle von ArrayList von HashMap. HashMap kann ein Mitglied von RestSection sein, wenn diese Daten benötigt werden, aber es scheint, dass dies durch eine einfache Zeichenfolge ersetzt werden kann. Die RestSection-Datenstruktur sollte auch ein Mitglied haben, das eine Liste von RestNotes darstellt. Die Artikelansicht für diesen Adapter sollte eine RecyclerView haben, die sich in Ihrem Fall in NoteFragment befindet. Dies ersetzt notetText in activity_group_section.xml. In der getView() Ihres Adapters sollten Sie den Adapter dieser RecyclerView mit den Notizen der Sektion einstellen. Weitere Informationen finden Sie in den folgenden Codefragmenten.

Datenstrukturen:

public class RestSection { 
    private int msectionid; 
    private String msectionname; 
    private int mxposition; 
    private int myposition; 
    private int msectionwidth; 
    private int msectionheight; 

    private List<RestNote> mnotes; 
} 

public class RestNote { 
    private int mnoticesid; 
    private String mnotetext; 
    private String mnotetag; 
    private String mnotecolor; 
    private double mnoteorder; 
} 

SectionLazyAdapter:

public class SectionLazyAdapter extends BaseAdapter{ 
    private List<RestSection> data; 

    //... 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.activity_group_section, null); 

     RestSection mySection = data.get(position); 

     TextView sectionName = (TextView)vi.findViewById(R.id.sectionname); // title 
     sectionName.setText(mySection.getSectionName()); 

     NoteFragments noteGridView = (NoteFragments) vi.findViewById(R.id.notegridfragment); 
     noteGridView.setRecyclerViewAdapter(new NoteAdapter(mySection.getNotes())); 
     return vi; 
    } 
} 

activity_group_section.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:orientation="horizontal" 
    android:padding="5dip" > 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="15dp" 
     android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/sectionimage" 
      android:layout_width="140dp" 
      android:layout_height="200dp" 
      android:scaleType="fitXY" 
      android:padding="5dp" 
      android:src="@drawable/sectionbackground" 
      /> 

     <TextView 
      android:id="@+id/sectionname" 
      android:layout_width="90dp" 
      android:layout_height="match_parent" 
      android:text="@string/textView" 
      android:visibility="visible" 
      android:gravity="center" 
      android:layout_gravity="center_horizontal|top" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:scrollHorizontally="true" 
      android:layout_marginTop="10dp" /> 
     <fragment android:name="com.mypackagename.NoteFragments" 
      android:layout_width="97dp" 
      android:layout_height="160dp" 
      android:id="@+id/notegridfragment" 
      android:layout_gravity="center_horizontal|bottom" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="30dp" /> 
    </FrameLayout> 
</RelativeLayout> 

2nd-Level (RecyclerView.Adapter + RecyclerView)

Dieser Adapter sollte eine Liste von Notizen als Datensatz aufnehmen (wie oben in AbschnittLazyAdapter gezeigt). Verwenden Sie die Notizdaten, um die Benutzeroberfläche wie zuvor zu füllen. Siehe Codefragmente unten.

NoteFragments:

public class NoteFragments extends Fragment { 
    private RecyclerView noteGridView; 
    // ... 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     // as before 
     noteGridView = (RecyclerView) view; 
    } 

    public void setRecyclerViewAdapter(NoteAdapter adapter){ 
     noteGridView.setAdapter(adapter); 
    } 
} 

NoteAdapter:

public class NoteAdapters extends RecyclerView.Adapter<NoteViewHolder> { 
    private List<RestNote> mItems; 

    public NoteAdapters(List<RestNote> notes) { 
     super(); 

     mItems = notes; 
    } 

    @Override 
    public void onBindViewHolder(NoteViewHolder holder, int position) { 
     // populate as before... make sure that RestNote has all of the data required 
    } 
} 

Sie natürlich müssen Ihre Daten in das gewünschte Format neu zu ordnen, aber ich denke, es ist einfacher, die Daten auf diese Weise angezeigt werden soll.

Ich hoffe, dass dies hilft.

+0

Ich werde es ausprobieren und Ihnen eine Antwort geben. Danke für die Information. Bitte beachten Sie, dass es einige Zeit dauern wird. –

Verwandte Themen