我想在我的Fragment中制作一个自定义的GridView,但是它不能长时间打开页面。
我想在片段中设置setText和按钮(活动中的片段
通过HTTPURL在自定义的gridview中使用许多图片。
但是现在我无法打开我的页面。我不知道该怎么解决。
我试图解决这个问题很长时间。
示例
main Activity
import android.content.ClipData;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class HomePage extends AppCompatActivity {
private BottomNavigationView mainnav;
private FrameLayout mainframe;
private SelectFragment selectFragment;
private MapFragment mapFragment;
private ChatFragment chatFragment;
private CommentFragment commentFragment;
private MemberFragment memberFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acthomepage);
inital();
mainnav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{ //找尋 menu item id
case R.id.nav_find:
setFragment(selectFragment);
return true;
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_comment:
setFragment(commentFragment);
return true;
case R.id.nav_chat:
setFragment(chatFragment);
return true;
case R.id.nav_member:
setFragment(memberFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame,fragment);
fragmentTransaction.commit();
}
public void inital() {
mainnav = findViewById(R.id.main_nav);
mainframe =findViewById(R.id.main_frame);
selectFragment =new SelectFragment();
mapFragment = new MapFragment();
chatFragment = new ChatFragment();
commentFragment = new CommentFragment();
memberFragment = new MemberFragment();
}
}
item_gridview_member.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/lblmember"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imgmember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
fragement_member.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MemberFragment">
<TextView
android:text="test"
android:id="@+id/lbltest_member"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="lougut"
android:id="@+id/btnlogout"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<GridView
android:id="@+id/gridmember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:numColumns="3"
android:verticalSpacing="3dp"
android:layout_marginBottom="3dp">
</GridView>
</LinearLayout>
package MmeberFactory
Cimage.java
import android.graphics.Bitmap;
public class CImage {
private String imagePath;
private Bitmap bitmap;
public CImage(String imagePath, Bitmap bitmap) {
this.imagePath = imagePath;
this.bitmap = bitmap;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
GridViewAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.yao.pm.R;
import java.util.List;
public class GridViewAdapter extends BaseAdapter {
private LayoutInflater myInflater;
private List<CImage> ImgList;
public GridViewAdapter(Context context,List<CImage> ImgList) {
myInflater = LayoutInflater.from(context);
this.ImgList = ImgList;
}
@Override
public int getCount() {
return ImgList.size();
}
@Override
public Object getItem(int position) {
return ImgList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null)
{
convertView = myInflater.inflate(R.layout.item_gridview_member,null);
holder = new ViewHolder();
holder.imgmember = convertView.findViewById(R.id.imgmember);
holder.lblmember = convertView.findViewById(R.id.lblmember);
convertView.setTag(holder);
}
CImage image = ImgList.get(position);
holder.imgmember.setImageBitmap(image.getBitmap());
holder.lblmember.setText(image.getImagePath());
if (image.getBitmap() == null) {
holder.imgmember.setImageResource(R.mipmap.ic_launcher);
} else {
holder.imgmember.setImageBitmap(image.getBitmap());
}
return convertView;
}
public class ViewHolder {
ImageView imgmember;
TextView lblmember;
}
}
MemberFragement.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.yao.pm.MemberImage.CImage;
import com.example.yao.pm.MemberImage.GridViewAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class MemberFragment extends Fragment {
private GridViewAdapter adapter;
private ArrayList<CImage> imageList;
public MemberFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_member, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
inital();
lbltest_member.setText("test111");
adapter = new GridViewAdapter(getActivity(),imageList);
gridmember.setAdapter(adapter);
// i think that the promlem in here but i dont know to solove it...
}
public void inital(){
lbltest_member = getActivity().findViewById(R.id.lbltest_member);
btnlogout = getActivity().findViewById(R.id.btnlogout);
gridmember = getActivity().findViewById(R.id.gridmember);
}
TextView lbltest_member;
Button btnlogout;
GridView gridmember;
}
Tank you.