Single.java
package baleli.movieapp.model;
/**
* Created by wim on 4/14/16.
*/
public class Single {
private int img;
private String title;
public Single() {
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
MainActivity
package baleli.movieapp;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import baleli.movieapp.fragments.FragmentSingle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = getSupportFragmentManager().findFragmentById(android.R.id.content);
if(fragment == null){
fragment = FragmentSingle.newInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, fragment, "")
.commit();
} else {
getSupportFragmentManager()
.beginTransaction()
.attach(fragment)
.commit();
}
}
}
窗口小部件/ GridMarginDecoration
package baleli.movieapp.widgets;
import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* Created by wim on 4/14/16.
*/
public class GridMarginDecoration extends RecyclerView.ItemDecoration {
private int left;
private int right;
private int top;
private int bottom;
public GridMarginDecoration(Context context, int left, int right, int top, int bottom) {
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
}
@Override
public void getItemOffsets(
Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(left, top, right, bottom);
}
}
片段/ FragmentSingle.java
package baleli.movieapp.fragments;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import baleli.movieapp.R;
import baleli.movieapp.adapter.SingleListAdapter;
import baleli.movieapp.model.Single;
import baleli.movieapp.widgets.GridMarginDecoration;
/**
* Created by docotel on 4/14/16.
*/
public class FragmentSingle extends Fragment implements SingleListAdapter.OnGridItemSelectedListener{
private RecyclerView lvSingle;
private GridLayoutManager gridLayoutManager;
private SingleListAdapter singleListAdapter;
private Context context;
public static FragmentSingle newInstance() {
return new FragmentSingle();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_single, container, false);
lvSingle = (RecyclerView) rootView.findViewById(R.id.lvSingle);
return rootView;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
singleListAdapter = new SingleListAdapter(this);
gridLayoutManager = new GridLayoutManager(context, 2);
lvSingle.setLayoutManager(gridLayoutManager);
lvSingle.addItemDecoration(new GridMarginDecoration(context, 2, 2, 2, 2));
lvSingle.setAdapter(singleListAdapter);
loadData();
}
private void loadData(){
List<Single> singleList = new ArrayList<>();
Single single;
int img[] = {R.drawable.tariq, R.drawable.tariq,
R.drawable.tariq, R.drawable.tariq,
R.drawable.tariq, R.drawable.tariq,
R.drawable.tariq, R.drawable.tariq};
String title[] = {"AKB48 43rd Single - Kimi wa Melody", "AKB48 42nd Single - Kuchibiru ni Be My Baby",
"AKB48 41st Single - Halloween Night", "AKB48 40th Single - Bokutachi wa Tatakawanai",
"AKB48 39th Single - Green Flash", "AKB48 38th Single - Kibouteki Refrain",
"AKB48 37th Single - Kokoro no Placard", "AKB48 36th Single - Labrador Retriever"};
for (int i = 0; i < img.length; i++){
single = new Single();
single.setImg(img[i]);
single.setTitle(title[i]);
singleList.add(single);
}
singleListAdapter.addAll(singleList);
}
@Override
public void onGridItemClick(View v, int position) {
Toast.makeText(context, singleListAdapter.getItem(position).getTitle(), Toast.LENGTH_SHORT).show();
}
}
适配器/ SingleListAdapter
package baleli.movieapp.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import baleli.movieapp.R;
import baleli.movieapp.model.Single;
/**
* Created by wim on 4/14/16.
*/
public class SingleListAdapter extends RecyclerView.Adapter<SingleListAdapter.SingleViewHolder>{
private List<Single> singleList;
private OnGridItemSelectedListener onGridItemSelectedListener;
public SingleListAdapter(OnGridItemSelectedListener onGridItemSelectedListener) {
this.onGridItemSelectedListener = onGridItemSelectedListener;
singleList = new ArrayList<>();
}
private void add(Single item) {
singleList.add(item);
notifyItemInserted(singleList.size() - 1);
}
public void addAll(List<Single> singleList) {
for (Single single : singleList) {
add(single);
}
}
public void remove(Single item) {
int position = singleList.indexOf(item);
if (position > -1) {
singleList.remove(position);
notifyItemRemoved(position);
}
}
public void clear() {
while (getItemCount() > 0) {
remove(getItem(0));
}
}
public Single getItem(int position){
return singleList.get(position);
}
@Override
public SingleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_single, parent, false);
final SingleViewHolder singleViewHolder = new SingleViewHolder(view);
singleViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int adapterPos = singleViewHolder.getAdapterPosition();
if (adapterPos != RecyclerView.NO_POSITION) {
if (onGridItemSelectedListener != null) {
onGridItemSelectedListener.onGridItemClick(singleViewHolder.itemView, adapterPos);
}
}
}
});
return singleViewHolder;
}
@Override
public void onBindViewHolder(SingleViewHolder holder, int position) {
final Single single = singleList.get(position);
holder.img.setImageResource(single.getImg());
holder.title.setText(single.getTitle());
}
@Override
public int getItemCount() {
return singleList.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
}
public class SingleViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView title;
public SingleViewHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img);
title = (TextView) itemView.findViewById(R.id.title);
}
}
public interface OnGridItemSelectedListener {
void onGridItemClick(View v, int position);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="baleli.movieapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
fragment_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/lvSingle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:padding="1dp"/>
</LinearLayout>
list_item_single.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:background="#40000000"
android:padding="8dp"
android:textColor="#FFFFFF"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>
我创建了带有片段和recyclerview的gridlayout。项目完成gradle没有错误。但是当我在genymotion中运行我的应用程序时,我收到以下错误。不幸的是,应用已停止。 我有Logcat的截图,我在这里发帖。
所以帮助我。怎么修。提前谢谢......
答案 0 :(得分:1)
您必须首先检查您是否拥有资源ID的天气,然后尝试在图像视图中进行设置。
SupportSQLiteOpenHelper
答案 1 :(得分:0)
Tariq Aziz。
尝试捕获异常,也许single.getImg()为null或为空。
@Override
public void onBindViewHolder(SingleViewHolder holder, int position) {
final Single single = singleList.get(position);
try {
holder.img.setImageResource(single.getImg());
} catch (Exception e) {
// Not found
}
holder.title.setText(single.getTitle());
}