第一个片段包含列表,当用户选择其中一个列表时,将显示另一个片段,但没有任何反应。 BTW第一个片段是Home.Home中的一个选项卡的一部分,其中包含两个选项卡,第一个片段是其中一个,即我在下面发布的代码。
public class Home_SpecialOffer extends Fragment {
static ConnectivityManager cm;
AlertDialog dialog2;
AlertDialog.Builder build;
ListView lvhomespecialoffer;
private ProgressDialog dialog;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_specialoffer, container, false);
cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);// checking
build = new AlertDialog.Builder(getActivity()); // connectivity
dialog = new ProgressDialog(getActivity());
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.setMessage("Loading. Please wait...");
// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
.defaultDisplayImageOptions(defaultOptions)
.build();
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
// there screen goes
// to next screen
// else shows
// message
.isConnectedOrConnecting()
|| cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting()) {
Log.e("cm value",
""
+ cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting());
ImageLoader.getInstance().init(config); // Do it on Application start
lvhomespecialoffer = (ListView) rootView.findViewById(R.id.lvhomespecialoffer);
new JSONTask().execute("this is my url");
}
else {
build.setMessage("This application requires Internet connection. Would you connect to internet ?");
build.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
build.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
});
dialog2 = build.create();
dialog2.show();
}
return rootView;
}
public class JSONTask extends AsyncTask<String,String, List<ProductModel> > {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
@Override
protected List<ProductModel> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
String finalJson = buffer.toString();
JSONArray json = new JSONArray(finalJson);
List<ProductModel> productModelList = new ArrayList<>();
Gson gson = new Gson();
for(int i=0; i<json.length(); i++) {
JSONObject finalObject = json.getJSONObject(i);
ProductModel productModel = gson.fromJson(finalObject.toString(), ProductModel.class);
productModelList.add(productModel);
}
return productModelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<ProductModel> result) {
super.onPostExecute(result);
dialog.dismiss();
ProductAdapter adapter = new ProductAdapter(getActivity(), R.layout.row, result);
View footer = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
lvhomespecialoffer.addFooterView(footer);
lvhomespecialoffer.setAdapter(adapter);
// TODO need to set data to the list
}
}
public class ProductAdapter extends ArrayAdapter {
private List<ProductModel> productModelList;
private int resource;
private LayoutInflater inflater;
public ProductAdapter(Context context, int resource, List<ProductModel> objects) {
super(context, resource, objects);
productModelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.imgimage = (ImageView)convertView.findViewById(R.id.imgimage);
holder.txtproduct = (TextView)convertView.findViewById(R.id.txtproduct);
holder.txtprice = (TextView)convertView.findViewById(R.id.txtprice);
holder.btnbuy = (Button)convertView.findViewById(R.id.btnbuy);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(productModelList.get(position).getImg_file(), holder.imgimage, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
holder.txtproduct.setText(productModelList.get(position).getProduct_title());
holder.txtprice.setText(("PHP ")+String.format("%.2f",(productModelList.get(position).getUnit_price())));
holder.btnbuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(getActivity(),
// productModelList.get(position).getProduct_title()+" is not yet available!", Toast.LENGTH_LONG).show();
// Create new fragment and transaction
Fragment newFragment = new ProductDetails();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.specialoffer, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
return convertView;
}
class ViewHolder{
private ImageView imgimage;
private TextView txtproduct;
private TextView txtprice;
private Button btnbuy;
}
}
}
答案 0 :(得分:1)
在res / layout文件中设置btnBuy按钮可单击。
<Button
android:id="@+id/btnBuy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"/>
答案 1 :(得分:0)
如果您在支持包中使用该片段,则必须使用getSupportFragmentManager()